C++ feof() function

The feof() function in C++ checks if the end of the file associated with the given file stream has been reached or not.

feof() prototype

int feof(FILE* stream);

The feof() function takes a file stream as argument and returns an integer value which specifies if the end of file has been reached.

It is defined in <cstdio> header file.

feof() Parameters

stream: The file stream who end is to be checked.

feof() Return value

The feof() function returns nonzero if the end has been reached, zero otherwise.

Example: How feof() function works

#include <cstdio>

using namespace std;

int main()
{
    int c;
    FILE *fp;
    fp = fopen("file.txt", "r");
    if (fp)
    {
        while(!feof(fp))
        {
            c = getc(fp);
            putchar(c);
        }
    }
    fclose(fp);
    return 0;
}

When you run the program, a possible output can be:

Welcome to Programiz.com
Did you find this article helpful?