feof
int feof (FILE * stream); | 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.
Because EOF is a valid code when working with binary files,
this function has to be called to check for the End Of File on this type
of files.
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>
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