cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : cstdio (stdio.h) : clearerr
 
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstdio (stdio.h)
functions:
· clearerr
· fclose
· feof
· ferror
· fflush
· fgetc
· fgetpos
· fgets
· fopen
· fprintf
· fputc
· fputs
· fread
· freopen
· fscanf
· fseek
· fsetpos
· ftell
· fwrite
· getc
· getchar
· gets
· perror
· printf
· putc
· putchar
· puts
· remove
· rename
· rewind
· scanf
· setbuf
· setvbuf
· sprintf
· sscanf
· tmpfile
· tmpnam
· ungetc
· vfprintf
· vprintf
· vsprintf
macro constants:
· EOF
· FILENAME_MAX
· NULL
· TMP_MAX
objects:
· stderr
· stdin
· stdout
types:
· FILE
· fpos_t
· size_t

-

clearerr function
void clearerr ( FILE * stream );
<cstdio>

Clear error indicators

Resets both the error and the EOF indicators of the stream.
When a stream function fails either because of an error or because the end of the file has been reached, one of these internal indicators may be set. These indicators remain set until either this, rewind, fseek or fsetpos is called.

Parameters

stream
Pointer to a FILE object that identifies the stream.

Return Value

None

Example

/* writing errors */
#include <stdio.h>
int main ()
{
  FILE * pFile;
  pFile = fopen("myfile.txt","r");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    fputc ('x',pFile);
    if (ferror (pFile))
      {
      printf ("Error Writing to myfile.txt\n");
      clearerr (pFile);
      }
    fgetc (pFile);
    if (!ferror (pFile))
      printf ("No errors reading myfile.txt\n"); 
    fclose (pFile);
  }
  return 0;
}

This program opens an existing file called myfile.txt for reading and causes an I/O error trying to write on it. That error is cleared using clearerr, so a second error checking returns false.

Output:

Error Writing to myfile.txt
No errors reading myfile.txt

See also

feof Check End-of-File indicator (function)
ferror Check error indicator (function)
rewind Set position indicator to the beginning (function)
© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us