Dotcpp  >  编程教程  >  stdio.h头文件  >  C语言ferror()函数:检测流上的错误

C语言ferror()函数:检测流上的错误

点击打开在线编译器,边学边练

函数名: ferror

头文件:<stdio.h>

函数原型: int ferror(FILE *stream);

功 能: 检测流上的错误,若检测到给定流上的错误返回非0值。

参数: stream是要检测流

返回值:成功   返回非0值 ,失败   返回0值    


程序例:  使用ferror函数检查p所指向的文件1.txt中有无读写错误,若有错误,则将错误信息输出 

#include<stdio.h>

int main(void){

   FILE *stream;

   stream = fopen("1.txt", "w");  //以只写的方式打开文件,此文件是不可读的 

   (void) getc(stream);  //读取文件的一个字符

   if (ferror(stream)){   //判断流中是否有错误

      printf("Error reading from 1.txt\n");

      clearerr(stream); //复位错误指针

   }

   fclose(stream);

   return 0;

}


运行结果:

Error reading from 1.txt



本文固定URL:https://www.dotcpp.com/course/499

C语言函数库
assert.h头文件
ctype.h头文件
float.h头文件
io.h头文件
math.h头文件
mem.h头文件
setjmp.h头文件
stdio.h头文件
stdlib.h头文件
sigal.h头文件
string.h头文件
time.h头文件
Dotcpp在线编译      (登录可减少运行等待时间)