Edit

Share via


clearerr

Resets the error indicator for a stream. A more secure version of this function is available; see clearerr_s.

Syntax

void clearerr(
   FILE *stream
);

Parameters

stream
Pointer to FILE structure.

Remarks

The clearerr function resets the error indicator and end-of-file indicator for stream. Error indicators aren't automatically cleared; once the error indicator for a specified stream is set, operations on that stream continue to return an error value until clearerr, fseek, fsetpos, or rewind is called.

If stream is NULL, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, this function sets errno to EINVAL and returns. For more information on errno and error codes, see errno constants.

A more secure version of this function is available; see clearerr_s.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Routine Required header
clearerr <stdio.h>

For more compatibility information, see Compatibility.

Example

// crt_clearerr.c
// This program creates an error
// on the standard input stream, then clears
// it so that future reads won't fail.

#include <stdio.h>

int main( void )
{
   int c;
   // Create an error by writing to standard input.
   putc( 'c', stdin );
   if( ferror( stdin ) )
   {
      perror( "Write error" );
      clearerr( stdin );
   }

   // See if read causes an error.
   printf( "Will input cause an error? " );
   c = getc( stdin );
   if( ferror( stdin ) )
   {
      perror( "Read error" );
      clearerr( stdin );
   }
   else
      printf( "No read error\n" );
}

Input

n

Output

Write error: No error
Will input cause an error? n
No read error

See also

Error handling
Stream I/O
_eof
feof
ferror
perror, _wperror