EOF and Feof () in the C language

Source: Internet
Author: User

Today, I met feof () to determine the end of a multi-output line of the problem, on the Internet to see a well-written article, turn around (transcription) to deepen the impression. Original address: http://blog.csdn.net/flyyyri/article/details/5084981

1, EOF is a macro definition in the standard library, #define EOF-1, is an int data in a 32-bit system, expressed as 0xffffffff,eof is not a character, nor is it actually present in the file. EOF not only indicates that the read file is at the end of the state, it can also represent the read and write errors in I/O operations (which can be detected with ferror ()) and the error state of some other associated operations;

2, feof () is used only to test the end of the flow file, when the end, return non 0, when the file internal position pointer to the end of the file is not immediately placed in the file structure of the end tag, only to perform a read file operation, the end flag is set, and then call feof will return to true.

Here is an explanation from Baidu Encyclopedia:

Feof (FP) has two return values: If the end of the file is encountered, the value of the function feof (FP) is a value other than 0, otherwise 0. EOF is a sign of the end of a text file.  In a text file, the data is stored in the form of a character's Ascⅱ code value, the Ascⅱ code of the normal character is from 32 to 127 (decimal), and the EOF 16 is 0xFF (decimal-1), so you can use EOF as the end-of-file flag. When data is stored in binary form in a file, there is a value of 1, so EOF is not used as the end flag for binary files. To solve this problem, ASCI C provides a feof function to determine whether the file is finished. The feof function can be used to determine both binary and text files. The solution to the feof () function to read more than one line: 1, for example, read the data at the end of the loop, before the code to read the data to operate. Assuming that the file pointer fp points to a file with the string "Hello" in the file, the following code will output a hello and output an end character EOF (EOF is the return value of the FGETC function, not EOF in the file):
    int c=0;      while (! feof (FP))    {        int c=fgetc (FP);        printf ("%c:\t%x\n", C,c);    }
View Code

The reason for this is that when the internal pointer points to the end, it also performs a read operation, the file end tag is set, and the following code outputs only "Hello" and does not output the file terminator:

    int C =         fgetc (FP);          while (! feof (FP))    {            printf ("%c:/t%x/n", c,c);        c=fgetc (FP);    }
View Code

When the internal pointer of the file points to the end position, a read operation is performed first, the file end tag is placed, and the while loop ends immediately.

2, under normal circumstances, read a line does not have much influence, in the loop outside the last record deleted can

(EXT) EOF and feof in C language ()

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.