Feof function re-reading

Source: Internet
Author: User

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.


Clearerr, feof, ferror, fileno-check and reset stream status

# Include <stdio. h>

Void clearerr (File * stream );
Int feof (File * stream );
Int ferror (File * stream );
Int fileno (File * stream );

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 ).

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 .)

The functions clearerr, feof, and ferror conform to X3.159-1989 ({{€{nsi C }{€ ?.

See also
Open (2), unlocked_stdio (3), stdio (3)



Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.