Use of feof () and EOF -- Determination of the end of a file in C

Source: Internet
Author: User
View stdio. h, you can see the following definitions: # define EOF (-1) # DEFINE _ ioeof 0x0010 # define feof (_ stream)-> _ flag & _ ioeof) we can see that the two methods have different principles. Here we will talk about the EOF and feof () macro definitions, which are described in our textbooks. EOF is a non-output character, so it cannot be displayed on the screen. Since the ASCII code of a character cannot contain-1, it is appropriate to define the EOF as-1. When the value of the read character is equal to EOF, it indicates that the read character is not a normal character but a file Terminator, but this applies to reading and writing text files. In binary files, information is stored in numerical values. The value of EOF may be the information in the binary file to be processed. This leads to the situation where useful data needs to be read but processed as "End of File. To solve this problem, C provides a feof () function that can be used to determine whether the file ends. Feof (FP) is used to test whether the current state of the file pointed to by FP is "End of File ". If yes, the value returned by the function is 1 (true); otherwise, 0 (false ). With these two definitions, the differences between binary files and text files must be vague (alas, I didn't understand them at the time ), now let's review the concepts of these two files. The C language supports streaming files, which are considered as a sequence composed of character (byte) data. Based on the data organization and operation form, it can be divided into ASCII files and binary files. An ASCII file is also called a text file. It stores one character in a byte storage unit (the ASCII code of the character is stored in the external storage, and each character occupies one byte ). Binary files store data in the memory in the original format on the disk. Because its external storage format is the same as the memory representation format, each character's ASCII code is also stored in the external storage. However, EOF can only be used for text files. In fact, it is not very accurate. You need to check the type of the defined variable. The following code can be used for both text and binary files: int C; while (C = fgetc (FP ))! = EOF) {printf ("% x/N", c);} If FF is read, since C is defined as int type, c = 0x000000ff actually, is not equal to EOF (-1 = 0 xffffffff), so it is not mistaken as the end of the file. However, if C is defined as char, obfuscation may occur. Char C; while (C = fgetc (FP ))! = EOF) {printf ("% x/N", c);} because an ascii code is stored in a text file, and FF in an ascii code indicates a null value (blank ), generally, this parameter is not used. Therefore, if the read object returns ff, it indicates that it has reached the end of the text file. 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) ;}multiple output ff, the reason is that 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 (FF) is output ). The correct statement should be: Char C; C = fgetc (FP); While (! Feof (FP) {printf ("% x/N", c); C = fgetc (FP );}

 

Use of feof () and EOF -- Determination of the end of a file in C

Related Article

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.