ferror() — Test for read and write errors

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 ferror(FILE *stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

int ferror_unlocked (FILE *stream);

General description

Tests for an error in reading from or writing to the specified stream. If an error occurs, the error indicator for the stream remains set until you close the stream, call rewind(), or call clearerr().

If a non-valid parameter is given to an I/O function, z/OS® XL C/C++ does not turn the error flag on. This case differs from one where parameters are not valid in context with one another.

ferror_unlocked() is functionally equivalent to ferror() 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 successful, ferror() returns a nonzero value to indicate an error for the stream pointed to by stream.

If unsuccessful, ferror() returns 0.

Example

CELEBF10
⁄* CELEBF10                                      

   This example puts data out to a stream and then checks that                  
   a write error has not occurred.                                              
                                                                                
 *⁄                                                                             
#include <stdio.h>                                                              
                                                                                
int main(void)                                                                  
{                                                                               
   FILE *stream;                                                                
   char *string = "Important information";                                      
   stream = fopen("myfile.dat","w");                                            
                                                                                
   fprintf(stream, "%s\n", string);                                             
   if (ferror(stream))                                                          
   {                                                                            
      printf("write error\n");                                                  
      clearerr(stream);                                                         
   }                                                                            
   if (fclose(stream))                                                          
      printf("fclose error\n");                                                 
}                                                                               

Related information