问题描述
已在此处提出有关此问题的问题C 中的文件结尾 (EOF)但它仍然不能完全解决我的问题.
A question about this has been asked hereEnd of File (EOF) in Cbut it still doesn't completely solve my problem.
EOF
在任何不是 stdin
的数据流中对我来说都是有意义的,例如,如果我有一些 data.txt
文件,fgetc()
将读取所有字符并到达文件末尾并返回 -1
.
EOF
makes sense to me in any datastream which is not stdin
, for example if I have some data.txt
file, fgetc()
will read all the chars and come to the end of file and return -1
.
我不明白的是stdin
中EOF
的概念.如果我使用getchar()
,它会等待我输入一些东西,所以如果没有写入,文件结束,(EOF
)不会自动返回?
What I don't understand is the concept of EOF
in stdin
. If I use getchar()
, it will wait for me to enter something, so if there is NOTHING written, End of File, (EOF
) is not returned automatically?
那么是不是只有用户才能通过+在stdin
中调用EOF
?如果是这样,那么在 stdin
中 EOF
的一些用途是什么?我猜它告诉程序继续阅读,直到用户调用文件结尾?是这个吗?
So is it that only the user can invoke EOF
in stdin
by pressing +?If so then what are some of the uses of EOF
in stdin
? I guess it tells the program to continue reading until the user invokes end of file? is this it?
谢谢
推荐答案
不,不是.它应该由用户发送.
No, it's not. It should be sent by the user.
那么是不是只有用户才能通过+在stdin
中调用EOF
?
是的,您可以使用可以在控制台中输入的特殊组合键为 stdin
设置 EOF
指示器,对于 linux 控制台,即 + 对于 Windows,它是 +.
Yes, you can set the EOF
indicator for stdin
with a special key combination you can input in the console, for linux console that is + and for windows it's +.
如果是这样,那么 EOF
在 stdin
中有哪些用途?我猜它告诉程序继续阅读,直到用户调用文件结尾?是这个吗?
它的使用取决于你是否指示用户显式输入 EOF
,例如,我认为 python
控制台会告诉你类似 按 Ctrl+D 或输入 quit() 退出
.
The use of it depends on whether you instruct the user to input the EOF
explicitly or not, for example, I think python
console will tell you something like Press Ctrl+D or type quit() to exit
.
而 EOF
不一定是 -1
它是一个宏,您应该始终使用它来测试 EOF
指标.更重要的是 EOF
不是一个字符,它是一个特殊的值,表示 End Of F文件指示器已设置.
And EOF
is not necessarily -1
it's a macro and you should always use it to test for the EOF
indicator. And more importantly EOF
is not a character, it's a special value that indicates that the End Of File indicator is set.
另外,getchar()
等价于 fgetc(stdin)
.
这篇关于标准输入中的文件结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!