这里有人问过这个问题 End of File (EOF) in C 但它仍然没有完全解决我的问题。
EOF
在任何不是 stdin
的数据流中对我来说都是有意义的,例如,如果我有一些 data.txt
文件,fgetc()
将读取所有字符并到达文件末尾并返回 -1
。
我不明白的是stdin
中EOF
的概念。如果我使用 getchar()
,它会等待我输入一些东西,所以如果没有写入任何内容,文件结尾 (EOF
) 不会自动返回吗?
那么是否只有用户可以通过按Ctrl+Z调用stdin
中的EOF
?
如果是这样,那么 stdin
中的 EOF
有哪些用途?我猜它告诉程序继续阅读,直到用户调用文件结尾?是这个吗?
谢谢
最佳答案
so if there is NOTHING written, End of File, (EOF) is not returned automatically?
不,不是。它应该由用户发送。
So is it that only the user can invoke
EOF
instdin
by pressing Ctrl+Z?
是的,您可以设置 EOF
stdin
的指标使用特殊的组合键,您可以在控制台中输入,对于 linux 控制台是 Ctrl+D ,对于 Windows 是 Ctrl+ Z.
If so then what are some of the uses of
EOF
instdin
? I guess it tells the program to continue reading until the user user invokes end of file? is this it?
使用它取决于你是否指示用户输入 EOF
明确或不明确,例如,我认为python
控制台会告诉你类似 Press Ctrl+D or type quit() to exit
的信息.
和EOF
不一定是 -1
这是一个宏,您应该始终使用它来测试 EOF
指标。更重要的是 EOF
不是字符,它是一个特殊值,表示 End Of F 文件指示器已设置。
此外,getchar()
相当于fgetc(stdin)
.
关于c - 标准输入中的文件结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28216437/