furthermore, the actual problem was that the OP was NOT checking for EOF at all. a file under 100 is going to produce what kind of results if it just keeps reading "empty space"?
so basically that is the issue here, how it gets done. It looks like their is more then one solution as long as the check for End Of File is included into it while reading the file. Because that is how it was designed to be. as even the feof shows that the End Of File is still being used to give indication that it has read the end of the file, stop trying to read it.Code:FILE *fp;
fp = fopen(argv[1], "r"); // the input file
while (!(feof(fp))) {
//char c;
int c;
int i = 0;
char string[101]; // max string length is 100. last spot reserved for '\0'
string[100] = '\0';
while ((c = fgetc(fp)) != ' ' && i < 100) {
string[i] = c;
i++;
}
printf("%s\n",string);
}
fclose(fp);
return 0;
}
where if the person is using a small buffer size then that check too needs to be placed into the equation along with EOF.
another example by someone else using this buffer check method. without using that you're saying has to be else it is wrong.
C Tutorial – File I/O (using text files) | CodingUnit Programming Tutorials
Code:while (fgets(buf,1000, ptr_file)!=NULL)
printf("%s",buf);