龟etc():这是不够的,只是检查EOF? [英] fgetc(): Is it enough to just check EOF?
本文介绍了龟etc():这是不够的,只是检查EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在网络上找到各种例子龟etc()
使用这样的:
In various examples found on the web fgetc()
is used like this:
FILE *fp = fopen(PATH, "r");
if (fp == NULL) {
perror("main");
exit(EXIT_FAILURE);
}
int ch;
while (ch = fgetc(fp) != EOF) {
// do something
}
但根据手册页,以龟etc()
But according to the manpage to fgetc()
如果出现读误差时,误差
对于流指标应设置,
龟etc()将返回EOF,[CX]和
应当设置errno以指示错误。
If a read error occurs, the error indicator for the stream shall be set, fgetc() shall return EOF, [CX] and shall set errno to indicate the error.
所以需要我也检查这个?怎么样?
So need I check this too? And how?
推荐答案
您可以用FERROR检查(3)中,而之后:
You can check it with ferror(3), right after the while:
while (EOF != (ch = fgetc(fp)))
// do something
if (ferror(fp) != 0)
// error handling
FERROR返回一个非零值,如果发生错误。
ferror returns a non-zero if an error occured.
如果你想使用FP后发生错误,则需要清除错误标志用的clearerr:
If you want use fp after an error occured, you'll need to clear the error flag with clearerr:
clearerr(fp);
这篇关于龟etc():这是不够的,只是检查EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文