feof() — Test end of file (EOF) indicator

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
Language Environment

both  

Format

#include <stdio.h>

int feof(FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

int feof_unlocked (FILE *stream);

General description

Indicates whether the EOF flag is set for the given stream pointed to by stream.

The EOF flag is set when the user attempts to read past the EOF. Thus, a read of the last character in the file does not turn the flag on. A subsequent read attempt reaches the EOF.

For HFS files, a simultaneous reader cannot see the extensions automatically. Use clearerr() is required to reset the EOF flag.

If the file has a simultaneous writer that extends the file, the flag can be turned on by the reader before the file is extended. After the extension becomes visible to the reader, a subsequent read will get the new data and set the flag appropriately (see fflush()). For example, if the read does not read past the EOF, the flag is turned off. If a file does not have a simultaneous writer that is extending the file, it is not possible to read past EOF.

A successful repositioning in a file (with fsetpos(), rewind(), fseek()) or a call to clearerr() resets the EOF flag. For a terminal file, when the EOF flag is set, subsequent reads will continue to deliver no data until the EOF flag is cleared. This can be accomplished by calling clearerr() or rewind().

The terminal can only read past the EOF after the rewind() function or the clearerr() function is called. The EOF flag is cleared by calling rewind(), fsetpos(), fseek(), or clearerr() for this stream.

feof_unlocked() is functionally equivalent to feof() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

If and only if the EOF flag is set for stream, feof() returns nonzero.

Otherwise, feof() returns 0.

Example

CELEBF09
/* CELEBF09                                      

   This example scans the input stream until it reads an EOF character.         

 */                                                                             
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
                                                                                
main() {                                                                        
                                                                                
      FILE *stream;                                                             
      int rc;                                                                   
      stream = fopen("myfile.dat","r");                                         
                                                                                
/* myfile.dat contains 3 characters "abc" */                                    
      while (1) {                                                               
         rc = fgetc(stream);                                                    
         if (rc == EOF) {                                                       
            if (feof(stream)) {                                                 
               printf("at EOF\n");                                              
               break;                                                           
            }                                                                   
            else {                                                              
               printf("error\n");                                               
               break;                                                           
            }                                                                   
         }                                                                      
         else                                                                   
            printf("read %c\n",rc);                                             
      }                                                                         
}                                                                               
Output
read a
read b
read c
at EOF