clearerr - C stdio.h

C examples for stdio.h:clearerr

Type

function

From


<cstdio>
<stdio.h>

Prototype

void clearerr ( FILE * stream );

Description

Resets both the error and the eof indicators of the stream.

When a i/o function fails, C sets internal indicators.

The value these indicators is cleared by calling clearerr function, or by a call to any of:

  • rewind,
  • fseek,
  • fsetpos and
  • freopen.

The I/O function could fail because of an error or because the end of the file has been reached.

Parameters

ParameterDescription
stream Pointer to a FILE object that identifies the stream.

Return Value

None

Example

The following program opens an existing file for reading and causes an I/O error trying to write on it.

That error is cleared using clearerr function, so a second error checking returns false.

Demo Code


#include <stdio.h>

int main (){//from  www.  ja v  a  2  s .c  om
  FILE * pFile;
  pFile = fopen("main.cpp","r");

  if (pFile==NULL) {
     perror ("Error opening file");
     return -1;
  }

  fputc ('a',pFile);
  if (ferror (pFile)) {
      printf ("Error Writing to main.cpp\n");
      clearerr (pFile);
  }
  fgetc (pFile);
  if (!ferror (pFile))
      printf ("No errors reading main.cpp\n");
  fclose (pFile);

  return 0;
}

Related Tutorials