In article <>
those who know me have no need of my name <not-a-real-> wrote:
>in comp.lang.c i read:
>>while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))
>the feof test is redundant.
Actually, it is not redundant: it is wrong.
If fgets() returns NULL, we know that getc(infp) -- or some equivalent
operation -- returned EOF, but there are *two* reasons that getc()
might return EOF. One is "end of file", which would also set the
EOF indicator on the stream, making the feof() call redundant in
this (usual) case. The other, however, is "error reading stream".
You might, for instance, have this occur when reading from a bad
floppy disk. In this case the EOF indicator is *not* set on the
stream. Instead, the error indicator -- the thing tested by
ferror(infp) -- is set.
Thus, when reading from a bad floppy, the loop:
while (fgets(buf, MAXLINE, infp) != NULL && !feof(infp))
may run forever, calling fgets(), getting NULL due to ferror(infp),
and then seeing that feof(infp) is still 0.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it
http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.