w3resource

C feof() function

C library function - feof()

The feof() function indicates whether the end-of-file flag is set for the given stream.

Syntax:

int feof(FILE *stream)

feof() Parameters:

Name Description Required /Optional
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream. Required

Return value from feof()

  • The feof() function shall return non-zero if and only if the end-of-file indicator is set for stream.

Example: feof() function

This example read a text file until it reads an end-of-file character.

#include <stdio.h>

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

Output:

c Programming1
c Programming2
c Programming3

C Programming Code Editor:

Previous C Programming: C clearerr()
Next C Programming: C ferror()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/stdio/c_library_method_feof.php