在循环中 fread 后检查 EOF
Check EOF after fread in a loop
我明白了为什么下面的循环给我 bad input
,重新 Why is “while ( !feof (file) )” always wrong?
do {
if (fread(buf, 1, siz, stdin) != siz) {
if (feof(stdin))
fputs("bad input\n", stderr);
else /* implying ferror(stdin) */
perror("fread() failed");
return EXIT_FAILURE;
}
use(buf);
} while (!feof(stdin));
是否不可避免地要编写一些辅助 function/weird 调用以正确(?)按照 ungetc(getchar(), stdin)
检查 EOF 还是有更好的方法?
我会做类似的事情
size_t nread;
while ((nread = fread(buf, 1, siz, stdin)) == siz) {
use(buf);
}
if (feof(stdin)) {
if (nread == 0) {
/* Normal EOF; do nothing. I would not actually
write this branch. :-) */
} else {
/* Short read. */
fputs("bad data\n", stderr); }
}
} else {
perror("fread");
}
无需使用 ungetc
检查文件结尾。相反,您可以将循环重写为以下内容:
while ( fread(buf, 1, siz, stdin) == siz )
{
use(buf);
}
//verify that the loop was terminated due to end-of-file and not due
//to stream error
if ( ferror(stdin) )
{
perror( "fread() failed" );
return EXIT_FAILURE;
}
//No stream error occurred, so the loop must have been terminated due
//to end-of-file. Therefore, everything is ok and we can continue
//running the program normally.
根据情况,您可能还想额外检查是否发生了部分读取,并将其视为错误。请参阅其他答案,了解如何做到这一点。
我明白了为什么下面的循环给我 bad input
,重新 Why is “while ( !feof (file) )” always wrong?
do {
if (fread(buf, 1, siz, stdin) != siz) {
if (feof(stdin))
fputs("bad input\n", stderr);
else /* implying ferror(stdin) */
perror("fread() failed");
return EXIT_FAILURE;
}
use(buf);
} while (!feof(stdin));
是否不可避免地要编写一些辅助 function/weird 调用以正确(?)按照 ungetc(getchar(), stdin)
检查 EOF 还是有更好的方法?
我会做类似的事情
size_t nread;
while ((nread = fread(buf, 1, siz, stdin)) == siz) {
use(buf);
}
if (feof(stdin)) {
if (nread == 0) {
/* Normal EOF; do nothing. I would not actually
write this branch. :-) */
} else {
/* Short read. */
fputs("bad data\n", stderr); }
}
} else {
perror("fread");
}
无需使用 ungetc
检查文件结尾。相反,您可以将循环重写为以下内容:
while ( fread(buf, 1, siz, stdin) == siz )
{
use(buf);
}
//verify that the loop was terminated due to end-of-file and not due
//to stream error
if ( ferror(stdin) )
{
perror( "fread() failed" );
return EXIT_FAILURE;
}
//No stream error occurred, so the loop must have been terminated due
//to end-of-file. Therefore, everything is ok and we can continue
//running the program normally.
根据情况,您可能还想额外检查是否发生了部分读取,并将其视为错误。请参阅其他答案,了解如何做到这一点。