Principle and usage of feof ()


1. What is feof()?

feof() is a function that detects the end of a file on the stream, and returns a non-zero value if the file ends, or 0

if not

is commonly used in file operations, where feof() is often used to determine whether a file is finished.


two, feof() classic error

according to the definition of this function, everyone is so commonly used, but such use, regardless of whether there is content in file, will be judged as “file isn’t empty.”

#include<stdio.h>
int main(void)
{
    FILE *p;
    p = fopen("open.txt", "r");
    if (feof(p))
    {
        printf("文件为空。");
    }
    else
    {
        printf("文件不为空。");
    }
    return 0;
 }

3. Principle of feof()

1.EOF

EOF is a computer term, short for End Of File, which in the operating system means that the source has no more data to read. Data sources are often referred to as files or streams. This character usually exists at the end of the text to indicate the end of the data.

The

definition means that the end of the document has a hidden character “EOF”, and when the program reads it, it knows that the file has reached the end. The while loop plus the EOF judgment is usually used as a marker for the end of the read.

The value

EOF is usually -1, but it varies depending on the system.

2.feof()

  • feof() principle:
    The

    • feof() function does not evaluate whether the file is empty by reading the EOF.
    • for feof(), it works by standing at the cursor and looking backwards to see if there are any characters left. If so, return 0; If not, return non-0. It doesn’t read the information, it just checks to see if there’s anything left behind the cursor.
  • use error analysis:
    • for an empty file, when the program opens it, its cursor stops at the beginning of the file, but since nothing is stored in the file (but EOF does exist), the entire file is stored as an EOF. When the program opens the file and calls Feof () directly, the function looks over its head from the cursor, sees EOF, and of course returns 0.

4. How to use properly

now that we understand the principle, how do we use it correctly?

#include<stdio.h>
int main(void)
{
    FILE *p;
    p = fopen("open.txt", "r");
    getc(p);
    if (feof(p))
    {
        printf("文件为空。");
    }
    else
    {
        rewind(p);//将光标跳回到文件开头
        int a;
        fscanf(p,"%d",&a);
        printf("%d", a);
    }
    return 0;
 }

analysis:

  • for files, whether empty files or files with information, when the file is opened and the cursor is at the default beginning, there is information behind the cursor. At this time, calling feof() to see if there is any content behind the cursor, it makes no sense.
  • so we need to find the difference from the same, first use getc(), read a character from the file, move the cursor back one character. The cursor of the space-time file has been moved to the end of EOF, and using Feof () returns 1. This is the correct use of feof().
  • note that the cursor must be returned to the beginning of the file, because the cursor was moved forward one bit when deciding whether the file was empty. The cursor must be restored to the beginning, so as to ensure the normal reading of the file.

Read More: