Feof () and EOF usage

Source: Internet
Author: User

Yesterday, a friend suddenly asked about the end of the file.ProgramProblem. When you use feof () to determine a file, copying will generate one more character.

 

This problem was emphasized by the teacher in class when I was a freshman, but at that time I only vaguely remembered it. I remember that if I used this function wrong, it would be a problem, the solution is to read it first and then judge whether the specific content is actually forgotten. In addition, EOF is often used, so this problem does not have a very good concept.


Now there are more C languages, so we should be able to understand the causes of this problem. So I found a lot of explanations on this issue on the Internet. A lot of explanations are true, but many of them are not cleared. After reading this, I feel like I am confused. However, I still learned a lot about it. Now I will write down my understanding and hope I can learn from each other.


 

View stdio. h and 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 program can be used for both text and binary files:

Int C;
While (C = fgetc (FP ))! = EOF)
{
Printf ("% x/N", C );
}
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;
While (C = fgetc (FP ))! = EOF)
{
Printf ("% x/N", C );
}
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 location pointer (FP-> _ PTR) reaches the end of the file, and then a 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 FF 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 (FF) is output ).

The correct statement should be:
Char C;
C = fgetc (FP );
While (! Feof (FP ))
{
Printf ("% x/N", C );
C = fgetc (FP );
}

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.