feof() - I/O Function

How is feof() used in C++?
How to check file position indicator for EOF?

Explanation

feof() is an I/O function that checks the file position indicator for the EOF of the associated stream. This check end of file indicator is generally set by a previous operation on the stream that reached the End-of-File. This function returns a non zero value if the EOF is reached, otherwise "0" is returned.

Syntax:


int feof ( FILE * stream );

Example :



#include <stdio.h>
int main()
{
long count = 0;
FILE * stri = fopen ("getceg.txt", "r");
if( ferror(stri) )
{
perror( "Error in File" );
}
while (!feof (stri))
{
fgetc (stri);
count++;
}
fclose (stri);
printf ("Total bytes read in the file: %d\n", count-1);
return 0;
}

Result :

Total bytes read in the file: 11

In the above example the "feof()" is used to find the total number of bytes read in a text file. The content of the text file is "ABCA1@@ABNM".

C++ Tutorial


Ask Questions

Ask Question