ferror ()- 测试读/写错误

格式

#include <stdio.h>
int ferror(FILE *stream);

语言级别

ANSI

线程安全

描述

ferror() 函数用于测试在读取或写入给定 时是否发生错误。 如果发生错误,那么 的错误指示符将保持设置,直到您关闭 ,调用 rewind() 函数或调用 clearerr() 函数为止。

返回值

ferror() 函数返回非零值以指示给定 上的错误。 返回值 0 表示未发生任何错误。

示例

此示例将数据输出到流,然后检查是否未发生写错误。
#include <stdio.h>
 
int main(void)
{
   FILE *stream;
   char *string = "Important information";
   stream = fopen("mylib/myfile","w");
 
   fprintf(stream, "%s\n", string);
   if (ferror(stream))
   {
      printf("write error\n");
      clearerr(stream);
   }
   if (fclose(stream))
      perror("fclose error");
}

相关信息