feof in c – feof C Library Function

feof in c: The function int feof(FILE *stream); checks the state of end-of-File indicator associated with stream. Once the file position indicator reaches the end of the file, any further read operations will return EOF.
End of file indicator for a stream can be cleared by a call to rewind, fseek, clearerr, freopen or fsetpos.

Function prototype of feof

int feof(FILE *stream);
  • stream : A pointer to a FILE object which identifies a stream.

Return value of feof

C feof: This function returns a non-zero value if the end of file indicator for the stream is set, zero is returned otherwise.

C program using feof function

feof c: The following program shows the use of feof function to traverse the file from start till end of the file. Let file “textFile.txt” contains “feof C Standard Library function” string. The content of this file will get printed by the following program.

feof C Library Function

#include <stdio.h>
 
int main(){
   FILE *file;
   int c;
   
   file = fopen("textFile.txt","r");
   if(file == NULL){
      perror("Error: Unable to open a file");
   } else {
       while(!feof(file)){
          c = fgetc(file);
          printf("%c", c);
       }
       fclose(file);
   }
    
   return(0);
}

Output

feof C Standard Library function