I know that feof() is not supposed to be used as a loop condition, but what is it supposed to be used for?
I know that feof() is not supposed to be used as a loop condition, but what is it supposed to be used for?
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
It can be -- just not the way it is typically attempted. Here is an example from the standard.Originally Posted by XSquaredAnother good note:Code:#include <stdio.h> /* ... */ int count; float quant; char units[21], item[21]; do { count = fscanf(stdin, "%f%20s of %20s", &quant, units, item); fscanf(stdin,"%*[^\n]"); } while (!feof(stdin) && !ferror(stdin));An end-of-file and a read error can be distinguished by use of the feof and ferror functions.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
The fgets function returns NULL in two cases : 1] If the end of file is reached and there are no more characters left to read or, 2] If an error is encountered while reading the file.Originally Posted by Dave_Sinkula
So, when we break out of the fgets loop we cannot be certain if we could read all the file sucessfully since the return value of fgets contains no such information.
So when can check the value of feof to check if the end of file was actually reached.
The one who says it cannot be done should never interrupt the one who is doing it.