Namespaces
Variants
Views
Actions

ferror

From cppreference.com
< c‎ | io
 
 
File input/output


Functions
File access
Direct input/output
Unformatted input/output
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)
Formatted input
Formatted output
File positioning
Error handling
ferror
Operations on files
 
Defined in header <stdio.h>
int ferror( FILE *stream );

Checks the given stream for errors.

Contents

[edit] Parameters

stream - the file stream to check

[edit] Return value

Nonzero value if the file stream has errors occurred, 0 otherwise

[edit] Example

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    FILE* fp = fopen("test.txt", "r");
    if(!fp) {
        perror("File opening failed");
        return EXIT_FAILURE;
    }
 
    int c; // note: int, not char, required to handle EOF
    while ((c = fgetc(fp)) != EOF) { // standard C I/O file reading loop
       putchar(c);
    }
 
    if (ferror(fp))
        puts("I/O error when reading");
    else if (feof(fp))
        puts("End of file reached successfully");
 
    fclose(fp);
}


[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • 7.21.10.3 The ferror function (p: 339)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.19.10.3 The ferror function (p: 305)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.9.10.3 The ferror function

[edit] See also

clears errors
(function) [edit]
checks for the end-of-file
(function) [edit]
displays a character string corresponding of the current error to stderr
(function) [edit]