clearerr()

Clears a stream's end-of-file and error status.

  #include <stdio.h>
  
  void clearerr(FILE *stream);    
Parameter

stream

A pointer to a FILE stream

Remarks

The clearerr() function resets the end-of-file status and error status for stream. The end-of-file status and error status are also reset when a stream is opened.

Listing: Example of clearerr() usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *f;

static char name[] = "myfoo";

char buf[80];

// create a file for output

if ( (f = fopen(name, "w")) == NULL) {

printf("Can't open %s.\n", name);

exit(1);

}

// output text to the file

fprintf(f, "chair table chest\n");

fprintf(f, "desk raccoon\n");

// close the file

fclose(f);

// open the same file again for input

if ( (f = fopen(name, "r")) == NULL) {

printf("Can't open %s.\n", name);

exit(1);

}

// read all the text until end-of-file

for (; feof(f) == 0; fgets(buf, 80, f))

fputs(buf, stdout);

printf("feof() for file %s is %d.\n", name, feof(f));

printf("Clearing end-of-file status. . .\n");

clearerr(f);

printf("feof() for file %s is %d.\n", name, feof(f));

// close the file

fclose(f);

return 0;

}

Output

chair table chest

desk raccoon

feof() for file myfoo is 256.

Clearing end-of-file status. . .

feof() for file myfoo is 0.