描述
C库函数 int ferror(FILE *stream) 对数据流进行错误测试。
声明
int ferror(FILE *stream)
参数
-
stream − 标识数据流的FILE对象指针。
返回值
If the error indicator associated with the stream was set, the function returns a non-zero value else, it returns a zero value.
示例
#include <stdio.h> int main() { FILE *fp; char c; fp = fopen("file.txt", "w"); c = fgetc(fp); if( ferror(fp) ) { printf("Error in reading from file : file.txt\n"); } clearerr(fp); if( ferror(fp) ) { printf("读文件出现错误: file.txt\n"); } fclose(fp); return(0); }
对上面程序编译并运行会得到如下输出,因为尝试读取以只写模式打开的文件。
Error reading from file "file.txt"
http://www.tanlooger.com/