icp/oer/courses/c-basics/sections/26-files/01-read/solution.c

31 lines
754 B
C

#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
FILE *file = fopen("Lorem_Ipsum.txt","r");
if(file)
{
fseek(file, 0 , SEEK_END);
int Filesize = ftell(file);
rewind(file);
char *Buffer = malloc(sizeof(char)*(Filesize+1));
Buffer[Filesize] = 0;
fread(Buffer, 1, Filesize, file);
// Now you have read the data into a buffer and you could mess with it, if you wanted to.
// For now were will just print it out;
printf("%s", Buffer);
// if you use the file the whole liftime of the program you actualy don't need to close it.
fclose(file);
}
else
{
// Here you could write your error handling or log the error;
}
}