C feof()

The feof() function defined in the stdio.h header file. It helps to test the EOF (End of File) indicator has been set for the given argument stream.


int feof(FILE *stream); #where stream should be a file pointer

 

feof() Parameters: 

The feof() function takes a single parameter.

Parameter Description Required / Optional
stream the stream whose end-of-file indicator is to be tested Required

feof() Return Value

The return value of feof() function is a non-zero if the EOF indicator associated with the stream is set otherwise return  zero. 

Input Return Value
EOF indicator is set non-zero
 EOF indicator is not set zero

Examples of feof()

Example 1: Working of feof() function in C?


#include <stdio.h>

int main () {
   FILE *pnt;
   int chr;
  
   pnt = fopen("testfile.txt","r");
   if(pnt == NULL) {
      perror("Error in opening file");
      return(-1);
   }
   
   while(1) {
      chr = fgetc(pnt);
      if( feof(pnt) ) { 
         break ;
      }
      printf("%c", chr);
   }
   fclose(pnt);
   
   return(0);
}

Output:


/* The content of the file is the output */
C programming library functions
Example 2: How feof()  works in C?

#include <stdio.h>

FILE *pnt = NULL; 
char chr[50]; 
pnt = fopen("myfile.txt","r"); 
if(pnt) 
    { 
         while(!feof(pnt)) 
         { 
             fgets(chr, sizeof(chr), pnt); 
             puts(chr); 
         } 
         fclose(pnt); 
   } 
    return 0; 

Output:


This is sample file
It contains some text data
-----------------------------
Process exited after 0.9847 seconds with return value zero