C examples for stdio.h:feof
function
<cstdio> <stdio.h>
int feof ( FILE * stream );
Checks whether the end-of-File indicator is set. Returning a non-zero if it is. This indicator is cleared by a call to
Parameter | Description |
---|---|
stream | Pointer to a FILE object that identifies the stream. |
A non-zero value is returned if the end-of-file indicator is set. Otherwise, zero is returned.
The following code opens the file, and counts the number of characters that it contains.
The program checks whether the end-of-file was reached, and if so, prints the total number of bytes read.
#include <stdio.h> int main (){/*from w w w. j a va 2s. co m*/ FILE * pFile; int n = 0; pFile = fopen ("main.cpp","rb"); if (pFile==NULL) { perror ("Error opening file"); return -1; } while (fgetc(pFile) != EOF) { ++n; } if (feof(pFile)) { puts ("End-of-File reached."); printf ("Total number of bytes read: %d\n", n); } else puts ("End-of-File was not reached."); fclose (pFile); return 0; }