char *loadTextFile(const char *filename)
{
FILE *fileh;
char *text = 0;
long filelength;
if((fileh=fopen(filename,"rb"))== 0)
printf("loadTextFile() - could not open file");
else
{
fseek(fileh, 0, SEEK_END);
filelength = ftell(fileh);
rewind(fileh);
text=(char *) smartmalloc((int)filelength + 1);
fread(text,(int)filelength, 1, fileh);
fclose(fileh);
text[filelength]=0;
}
printf(text);
return(text);
}
This function only returns partial data of a txt file. It is also inconsistent…soemtimes gives me 100 characters of the file some times 20. I don’t see anything wrong with it. Thought I might get another pair of eyes on it. Thanks.
Here is a slightly better version of your code. You need more error checking with the IO function calls. Also, there is the annoying
longtosize_timplicit conversions which I would recommend dealing with properly in production code.Note that, John R. Strohm is right: If your assessment of what has been read is based on what
printfprints, then you are likely being misled by embeddednuls.