Determine the end of the file, feof ......

Source: Internet
Author: User

Because an ascii code is stored in a text file, and FF in the ASCII Code represents a null value (blank), it is generally not used, so if the read file returns ff, the end of the text file is reached. However, if it is a binary file, which may contain ff, you cannot use the read EOF as the condition for end of the file. At this time, you can only use the feof () function.

In VC, only when the file position pointer (FP-> _ PTR) reaches the end of the file and then the read/write operation occurs, the flag (FP-> _ flag) to include _ ioeof. Then call feof () to obtain the end information of the file. Therefore, if you run the following program:

Char C; while (! Feof (FP) {c = fgetc (FP); printf ("% x \ n", c );}

 

One more ffffffff is output, because after reading the last character, FP-> flag is still not set to _ ioeof, so feof () still does not detect the end of the file. Feof () cannot detect the end of a file until fgetc () is called again. In this way, one more-1 (ffffffff) is output ). The correct statement should be:

Char C; C = fgetc (FP); While (! Feof (FP) {printf ("% x \ n", c); C = fgetc (FP );}

 

In fact, feof () can be replaced by EOF? No, there is another problem. When fgetc returns-1, there are two situations: Reading the end of a file or reading an error. Therefore, we are not sure that the file has ended, because it may be a read error! In this case, we need feof ().

Determine the end of the file, feof ......

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.