Do you really know about EOF and feof?

Source: Internet
Author: User

There are two ways to determine the end of a file: EOF and Feof ()
To view stdio.h you can see the following definition:
#define EOF (-1)

#define _IOEOF 0x0010
#define FEOF (_stream) (_stream)->_flag & _ioeof)

From this we can see that the principles of these two methods are different.
It is said that EOF can only be used for text files, but it does not have to look at the types of variables defined. The following procedure is available for both text and binary files:
int C;
while ((C=FGETC (FP))!= EOF)
{
printf ("%x/n", c);
}
If FF is read, because C is defined as int, it is actually c=0x000000ff, not equal to EOF ( -1=0XFFFFFFFF), and therefore does not misjudge to the end of the file. However, if C is defined as a char type, it is possible to create confusion.
char c;
while ((C=FGETC (FP))!= EOF)
{
printf ("%x/n", c);
}
Because the ASCII code is stored in the text file and the FF represents the null value (blank) in the ASCII code, it is generally not used, so if the read file returns FF, the description is already at the end of the text file. However, if it is a binary file, it may contain FF, so it is not possible to read EOF as the end of a file, and only the feof () function can be used at this time.

In VC, only when the file position pointer (fp->_ptr) to the end of the file, and then the read/write operation, the flag bit (Fp->_flag) will be placed to contain _ioeof. Then call feof () to get the file end information. Therefore, if you run the following program:
char c;
while (!feof (FP))
{
c = fgetc (FP);
printf ("%x/n", c);
}
It is found that one 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 the fgetc () is called again to perform a read operation. This will output more than one-1 (that is, FF).
The correct wording should be:
char c;
c = fgetc (FP);
while (!feof (FP))
{
printf ("%x/n", c);
c = fgetc (FP);
Note that this is the correct solution, there is a mistake in Tan Haoqiang's book because I did not notice the problem, below please see

This file copies the program code, red indicates the error

Wrong solution: Note the red part #include<stdio.h>
#include <stdlib.h>
Main ()
{
FILE *in,*out;

if ((In=fopen ("In.txt", "R")) ==null)
{
printf ("Cannot open file");
Exit (0);
}
if ((Out=fopen ("OUT.txt", "W")) ==null)
{
printf ("Cannot open file");
Exit (0);
}

while (!feof (in))
{
FPUTC (Fgetc (in), out);

}
Fclose (in);
Fclose (out);
}

Look at the correct code:

#include <stdio.h>
#include <stdlib.h>
Main ()
{
FILE *in,*out;
Char ch;

if ((In=fopen ("In.txt", "R")) ==null)
{
printf ("Cannot open file");
Exit (0);
}
if ((Out=fopen ("OUT.txt", "W")) ==null)
{
printf ("Cannot open file");
Exit (0);
}
Ch=fgetc (in);
while (!feof (in))
{
FPUTC (ch,out);
Ch=fgetc (in);
}
/*while ((Ch=fgetc (in))!=eof)
FPUTC (ch,out), this code is also the correct CH character variable can only be applied to text files, *

Fclose (in);
Fclose (out);
}

So actually feof () can be replaced by EOF. No, there's another problem here. When Fgetc returns-1, there are two scenarios: reading to the end of a file or reading an error. So we can't be sure that the file is over because it might be a read error! Then 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.