Advertisement
okpalan

alice.txt

Dec 19th, 2023
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main() {
  5.     FILE *fp;
  6.     char *buffer;
  7.     long file_size;
  8.  
  9.     fp = fopen("alice.txt", "rb");
  10.     if (fp == NULL) {
  11.         printf("Error: Unable to open file.\n");
  12.         return 1;
  13.     }
  14.  
  15.     fseek(fp, 0, SEEK_END);
  16.     file_size = ftell(fp);
  17.     rewind(fp);
  18.  
  19.     buffer = (char *)malloc((file_size + 1) * sizeof(char));
  20.     fread(buffer, file_size, 1, fp);
  21.     buffer[file_size] = '\0';
  22.  
  23.     printf("File contents:\n%s\n", buffer);
  24.  
  25.     fclose(fp);
  26.     free(buffer);
  27.  
  28.     return 0;
  29. }
  30.  
Tags: alice.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement