Hi!
I have to read a huge data file (~180MB) of unkown size into a char array. I am really not sure how to go about doing this... This is what I have so far:
Code:
FILE *input;
int count;
char *cFile;
long filelength;
/* file input */
if ( (input = fopen("inputfile", "rb" )) == NULL)
{
printf("file not found");
getchar();
exit(1);
}
// @ checking length of file
fseek(input, 0L, SEEK_END);
filelength = ftell(input);
rewind(input);
// read the file into memory
cFile = calloc(filelength + 1, sizeof(char)); //allocate memory for file
if(cFile == NULL)
{
printf("\nInsufficient memory to read file\n");
getchar();
exit(1);
}
//read file into allocated memory
if (fread(cFile, filelength, 1, input) != 1)
{
printf("error reading file");
exit(1);
}
fclose(input);
Does anyone have any better suggestions of how to better approach this issue? Right now I don't have the exact file I will be using (180MB of binary data) so I can only test with smaller samples. I am worried once I get the BIG file I won't have sufficient memory=(
All comments/suggestions appreciated.