Latest web development tutorials

C library functions - clearerr ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsvoid clearerr (FILE * stream) to clear the end of the file given flow stream and error identifier.

statement

Here is () statement clearerr function.

void clearerr(FILE *stream)

parameter

  • stream - This is a pointer to FILE pointer to an object, the object identifier FILE flow.

return value

This will not fail and do not set the external variable errno, but if it detects that its argument is not a valid stream, it returns -1 and set errno to EBADF.

Examples

The following example demonstrates clearerr () function is used.

#include <stdio.h>

int main()
{
   FILE *fp;
   char c;

   fp = fopen("file.txt", "w");

   c = fgetc(fp);
   if( ferror(fp) )
   {
      printf("读取文件:file.txt 时发生错误\n");
   }
   clearerr(fp);
   if( ferror(fp) )
   {
      printf("读取文件:file.txt 时发生错误\n");
   }
   fclose(fp);

   return(0);
}

Suppose we have a text filefile.txt, which is an empty file.Let's compile and run the above program, because we are trying to read a write-only mode to open the file, which will produce the following results.

读取文件:file.txt 时发生错误

C standard library - <stdio.h> C standard library - <stdio.h>