Advertisement
lithox2g

input_to_string.c

Nov 29th, 2022
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.49 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6.     char *content;
  7.     long filesize;
  8.     FILE *fp = fopen("input.txt", "rb");
  9.     if (fp == NULL)
  10.     {
  11.         return 1;
  12.     }
  13.     else
  14.     {
  15.         fseek(fp, 0, SEEK_END);
  16.         filesize = ftell(fp);
  17.         rewind(fp);
  18.         content = malloc((filesize + 1) * (sizeof(char)));
  19.         fread(content, sizeof(char), filesize, fp);
  20.         fclose(fp);
  21.         content[filesize] = 0;
  22.     }
  23.     printf("%s\n", content);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement