C programming clearerr stdio.h


In C programming the <stdio.h> clearerr function clears the end-of-file and error indicators for the stream.The declaration of the function is given below.

void clearerr(FILE *stream);

Parameters:
stream -The pointer to the file whose EOF and error indicator is to be cleared.

Return type
void

A code example is given below.

Code example

FILE *fp=fopen(“text.bin” , “rb+”);
int val , count ;

if (fp != NULL)
{
while ( true) //read text.bin file
{
val=fgetc(fp); //read character from the file
if( val==EOF)
{
 if( feof(fp) )
 {
 fprintf(stderr , “\nEnd of file has reached”);
 break;
 }
 else if ( ferror(fp) )
 {
fprintf( stderr, “\nAn error has occurred”);
 clearerr(fp)
 break;
 }
}
else
 printf(“%c”, val); //print the character or process the character

}
else
printf(“File cannot be opened”);

fclose( fp )

The above program read character from the file and checks if EOF has reached or an error has occurred while reading.


Related links

->C ferror

->C perror

->C feof