There are two methods to determine the end of a file: EOF and feof ()

Source: Internet
Author: User

There are two methods to determine the end of a file: EOF and feof ()

 

View stdio. h and you can see the following definitions:

# Define EOF (-1) <br/> # DEFINE _ ioeof 0x0010 <br/> # define feof (_ stream) (_ Stream) -> _ flag & _ ioeo

We can see that the two methods have different principles.
Some people say that EOF can only be used in text files. In fact, it is not the case, but also the type of the defined variable. The following program can be used for both text and binary files:

Int C; <br/> while (C = fgetc (FP ))! = EOF) <br/>{< br/> printf ("% x/N", c); <br/>}

If FF is read and C is defined as int type, c = 0x000000ff is not equal to EOF (-1 = 0 xffffffff). Therefore, it is not mistaken as the end of the file.
However, if C is defined as char, obfuscation may occur.

Char C; <br/> while (C = fgetc (FP ))! = EOF) <br/>{< br/> printf ("% x/N", c); <br/>}

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; <br/> while (! Feof (FP) <br/>{< br/> C = fgetc (FP); <br/> printf ("% x/N", C ); <br/>}

One more ffffffff is output, because after reading the last character, FP-> flag is still not set to _ ioeof, so feof () still does not
The end of the file is detected. 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; <br/> C = fgetc (FP); <br/> while (! Feof (FP) <br/>{< br/> printf ("% x/N", c); <br/> C = fgetc (FP ); <br/>}
 


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

 

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.