Namespaces
Variants
Views
Actions

clearerr

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
clearerr
Operations on files
 
Defined in header <stdio.h>
void clearerr( FILE *stream );

Resets the error flags and the EOF indicator for the given file stream.

Contents

[edit] Parameters

stream - the file to reset the error flags for

[edit] Return value

(none)

[edit] Example

clearerr resets the EOF indicator.

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    FILE* tmpf = tmpfile();
    fputs("abcde\n", tmpf);
    rewind(tmpf);
 
    int ch;
    while ((ch=fgetc(tmpf)) != EOF)   /* read/print characters including newline */
          printf("%c", ch);
 
    /* Test reason for reaching EOF. */
    if (feof(tmpf))          /* if failure caused by end-of-file condition */
    {
       puts("End of file reached");
       clearerr(tmpf);  /* clear eof indicator */
       if (feof(tmpf))
          printf("EOF indicator set\n");
       else
          printf("EOF indicator cleared\n");
    }
    else if (ferror(tmpf))   /* if failure caused by some other error      */
         {
            perror("fgetc()");
            fprintf(stderr,"fgetc() failed in file %s at line # %d\n", __FILE__,__LINE__-16);
            clearerr(tmpf);  /* clear error indicator */
            if (ferror(tmpf))
               printf("error indicator set\n");
            else
               printf("error indicator cleared\n");
            exit(EXIT_FAILURE);
         }
 
    return EXIT_SUCCESS;
}

Output:

abcde
End of file reached
EOF indicator cleared

[edit] Example

clearerr resets error indicator.

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    FILE* tmpf = tmpfile();
    fputs("abcde\n", tmpf);
    fclose(tmpf);            /* close temporary file to force error */
 
    int ch;
    while ((ch=fgetc(tmpf)) != EOF)   /* read/print characters including newline */
          printf("%c", ch);
 
    /* Test reason for reaching EOF. */
    if (feof(tmpf))          /* if failure caused by end-of-file condition */
    {
       puts("End of file reached");
       clearerr(tmpf);  /* clear eof indicator */
       if (feof(tmpf))
          printf("EOF indicator set\n");
       else
          printf("EOF indicator cleared\n");
    }
    else if (ferror(tmpf))   /* if failure caused by some other error      */
         {
            perror("fgetc()");
            fprintf(stderr,"fgetc() failed in file %s at line # %d\n", __FILE__,__LINE__-16);
            clearerr(tmpf);  /* clear error indicator */
            if (ferror(tmpf))
               printf("error indicator set\n");
            else
               printf("error indicator cleared\n");
            exit(EXIT_FAILURE);
         }
 
    return EXIT_SUCCESS;
}

Output:

fgetc(): Bad file descriptor
fgetc() failed in file main.cpp at line # 11
error indicator cleared

[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • 7.21.10.1 The clearerr function (p: 338)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.19.10.1 The clearerr function (p: 304)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 4.9.10.1 The clearerr function

[edit] See also

checks for the end-of-file
(function) [edit]
displays a character string corresponding of the current error to stderr
(function) [edit]
checks for a file error
(function) [edit]
C++ documentation for clearerr