ferror()函数
语法:
1 2 |
?? #include <stdio.h> ?? int ferror( FILE *stream ); |
ferror()函数检查stream(流)中的错误, 如果没发生错误返回0,否则返回非零. 如果发生错误, 使用perror()检测发生什么错误.
error,函数名,在调用各种输入输出函数(如 putc.getc.fread.fwrite等)时,如果出现错误,除了函数返回值有所反映外,还可以用ferror函数检查。 它的一般调用形式为 ferror(fp);如果ferror返回值为0(假),表示未出错。如果返回一个非零值,表示出错。应该注意,对同一个文件 每一次调用输入输出函数,均产生一个新的ferror函 数值,因此,应当在调用一个输入输出函数后立即检 查ferror函数的值,否则信息会丢失。在执行fopen函数时,ferror函数的初始值自动置为0。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<stdio.h> int main(void) { FILE*stream; /*openafileforwriting*/ stream= fopen("DUMMY.FIL","w"); /*forceanerrorconditionbyattemptingtoread*/ (void) getc(stream); if(ferror(stream))/*testforanerroronthestream*/ { /*displayanerrormessage*/ printf("ErrorreadingfromDUMMY.FIL\n"); /*resettheerrorandEOFindicators*/ clearerr(stream); } fclose(stream); return0; } |