When fgets reads the last row of the file (the returned result is not null), feof test is false, fgets is performed once (the returned result is null), and feof test is true.
Feof is used to judge whether the stream has reached the end by judging whether an error is returned by read.
When performing a flow operation, you should determine that fgets () is null.
In fact, it is easy to correctly use the feof function. Just remember the principle: "read the file first and then use feof to judge ".
Foef (Stream) is used to indicate the end Of the file with an indicator (indicator), and feof checks whether the indicator is set, rather than determining whether the current seek value reaches the end.
When the seek value reaches the end, the "stream" is not necessarily the end of the file, because the "stream" will continue to grow.
For the red part, some code does.
While (! Feof (FP) <br/> printf ("this file has EOF/N"); <br/> lseek (FP, 0, seek_set );
Check whether there is an EOF identifier before processing.
Name
Clearerr, feof, ferror, fileno-check and reset stream status
Synopsis
# Include <stdio. h>
Void clearerr (File * stream );
Int feof (File * stream );
Int ferror (File * stream );
Int fileno (File * stream );
Description
The function clearerr clears the end-of-file and error indicators for the stream pointed to by stream.
The function feof tests the end-of-file indicator for the stream pointed to by stream, returning non-zero if it is
Set. The end-of-file indicator can only be cleared by the function clearerr.
The function ferror tests the error indicator for the stream pointed to by stream, returning non-zero if it is set.
The error indicator can only be reset by the clearerr function.
The function fileno examines the argument stream and returns its integer descriptor.
For non-locking counterparts, see unlocked_stdio (3 ).
Errors
These functions shocould not fail and do not set the external variable errno. (However, In case fileno detects that
Its argument is not a valid stream, it must return-1 and set errno to ebadf .)
Conforming
The functions clearerr, feof, and ferror conform to X3.159-1989 ({{€{nsi C }{€ ?.
See also
Open (2), unlocked_stdio (3), stdio (3)