Latest web development tutorials

C library functions - ferror ()

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

description

C library functionsint ferror (FILE * stream) test given flow stream error identifier.

statement

Here is () statement ferror function.

int ferror(FILE *stream)

parameter

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

return value

If the error identifier associated with the flow, the function returns a non-zero value, otherwise it returns a value of zero.

Examples

The following example demonstrates ferror () 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>