C语言 在不移动文件指针的情况下检查文件指针是否已达到 EOF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18051110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking if the file pointer has reached EOF without moving the file pointer?
提问by Karim Elsheikh
my question is simple, but I can't find it on google, so here I am.
我的问题很简单,但我在谷歌上找不到,所以我来了。
Basically, as an example, I am reading in a bunch of integers until EOF from an input file. I used fgetc to check, but then it moves the file pointer to the address of the 2nd integer. How can I check for EOF in this while loop without moving the file pointer when it checks?
基本上,作为一个例子,我正在从输入文件中读取一堆整数,直到 EOF。我使用 fgetc 进行检查,但随后它将文件指针移动到第二个整数的地址。如何在此 while 循环中检查 EOF 而不在检查时移动文件指针?
Keep in mind I will be doing something much more complex with this loop than scanning in integers. Also, don't mention my use of fscanf rather than fgets, I know. Its just a simple example to show you what I mean.
请记住,我将用这个循环做一些比扫描整数更复杂的事情。另外,我知道,不要提及我使用 fscanf 而不是 fgets。它只是一个简单的例子,向你展示我的意思。
while(fgetc(ifp) != EOF)
{
fscanf(ifp, "%d", &test);
printf("%d\n", test);
}
If the input file has integers 1-10 for example, the above code would print:
例如,如果输入文件有整数 1-10,上面的代码将打印:
2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10
Missing the 1!
缺1!
回答by Dayal rai
fgetcReturns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced to the next character.Using do{ }while();will solve your problem.
fgetc返回指定流的内部文件位置指示符当前指向的字符。内部文件位置指示器然后前进到下一个字符。使用do{ }while();将解决您的问题。
回答by luser droog
You should consider EOF conditions to be an implementation detail. What's really important here is whether fscanf has successfully returned a value.
您应该将 EOF 条件视为实现细节。这里真正重要的是 fscanf 是否成功返回了一个值。
while(fscanf(ifp, "%d", &test) == 1) {
printf("%d\n", test);
}
On the other hand, this feels like one of those times where it makes sense to put the logic in the middle of the loop.
另一方面,这感觉像是将逻辑放在循环中间的那些时候。
while(1) {
int ret = fscanf(ifp, "%d", &test);
if (ret != 1)
break;
printf("%d\n", test);
}
Especially for debugging, it can be nice to split things up into separate statements with variables that can be inspected.
特别是对于调试而言,将事情拆分为带有可以检查的变量的单独语句会很好。
回答by Christoph
For various reasons, it's impossible to determine if EOF has been reached without actually performing a prior read from the file.
由于各种原因,如果不实际从文件中执行读取操作,就不可能确定是否已达到 EOF。
However, using a seperate function call to do this is a bad idea, even if you were to use ungetc()to put back the character you tried to read.
但是,使用单独的函数调用来执行此操作是一个坏主意,即使您要使用ungetc()放回您尝试读取的字符也是如此。
Instead, check the return value of the call to fscanf()itself:
相反,检查对fscanf()自身调用的返回值:
while(fscanf(ifp, "%d", &test) == 1)
{
printf("%d\n", test);
}
回答by verbose
How about simply:
简单地说:
while (fscanf (ifp, "%d", &test) != EOF)
printf("%d\n", test);
回答by P0W
fgetc(ifp)advance the file pointer, hence skipping your first integer
fgetc(ifp)推进文件指针,因此跳过你的第一个整数
Instead Use: -
而是使用: -
while(1)
{
if(fscanf(ifp, "%d", &test)!=1)
break;
printf("%d\n", test);
}

