cplusplus.com > reference > cstdio > feof |
feof
<stdio.h> |
cplusplus.com |
Check if End Of File has been reached.
Tests if the position indicator of a given stream has reached
the End of File.
Once End Of File has been reached, further reading operations
on the same stream return EOF until rewind() or
fseek() are called changing the position indicator to a valid position.
Because the EOF value returned by many input functions may indicate either the End-Of-File or an error,
this function should be called to ensure the End Of File has been reached.
Parameters.
Return Value.
A non-zero value is returned in the case that the position indicator
reached the End Of File in the last input operation
with the specified stream, otherwise 0 is returned.
Example.
/* feof example: byte counter */
#include <stdio.h>
int main ()
{
FILE * pFile;
long n = 0;
pFile = fopen ("myfile.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
while (!feof(pFile)) {
fgetc (pFile);
n++;
}
fclose (pFile);
printf ("Total number of bytes: %d\n",n);
}
return 0;
}
This code opens a file called myfile.bin, and counts the number
of characters that it contains by reading them one by one.
Finaly the total amount of bytes is printed out.
See also.
clearerr,
ferror,
fwrite