C语言 标准输入中的文件结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28216437/
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
End of File in stdin
提问by Mcs
A question about this has been asked here End of File (EOF) in Cbut it still doesn't completely solve my problem.
在 C 中的文件结尾 (EOF) 中已经提出了一个关于此的问题 ,但它仍然没有完全解决我的问题。
EOFmakes sense to me in any datastream which is not stdin, for example if I have some data.txtfile, fgetc()will read all the chars and come to the end of file and return -1.
EOF对我来说很有意义的任何数据流是不是stdin,例如,如果我有一些data.txt文件,fgetc()将读取所有的字符和前来文件和回复结束-1。
What I don't understand is the concept of EOFin 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?
我不明白的是EOFin的概念stdin。如果我使用getchar(),它会等待我输入一些东西,所以如果没有写,文件结束,( EOF) 不会自动返回?
So is it that only the user can invoke EOFin stdinby pressing Ctrl+Z?
If so then what are some of the uses of EOFin stdin? I guess it tells the program to continue reading until the user invokes end of file? is this it?
因此,它是只有用户可以调用EOF在stdin按Ctrl+ Z?如果是这样,那么EOFin 的一些用途是stdin什么?我猜它告诉程序继续阅读,直到用户调用文件结尾?是这个吗?
Thank you
谢谢
回答by Iharob Al Asimi
so if there is NOTHING written, End of File, (EOF) is not returned automatically?
所以如果没有写,文件结束,(EOF)不会自动返回?
No, it's not. It should be sent by the user.
不,这不对。它应该由用户发送。
So is it that only the user can invoke
EOFinstdinby pressing Ctrl+Z?
因此,它是只有用户可以调用
EOF在stdin按Ctrl+ Z?
Yes, you can set the EOFindicator for stdinwith a special key combination you can input in the console, for linux console that is Ctrl+Dand for windows it's Ctrl+Z.
是的,您可以使用可以在控制台中输入的特殊组合键来设置EOF指示器stdin,对于 linux 控制台是Ctrl+ D,对于 Windows 是Ctrl+ Z。
If so then what are some of the uses of
EOFinstdin? I guess it tells the program to continue reading until the user user invokes end of file? is this it?
如果是这样,那么
EOFin 的一些用途是stdin什么?我猜它告诉程序继续阅读,直到用户调用文件结尾?是这个吗?
The use of it depends on whether you instruct the user to input the EOFexplicitly or not, for example, I think pythonconsole will tell you something like Press Ctrl+D or type quit() to exit.
它的使用取决于您是否指示用户EOF明确输入,例如,我认为python控制台会告诉您类似Press Ctrl+D or type quit() to exit.
And EOFis not necessarily -1it's a macro and you should always use it to test for the EOFindicator. And more importantly EOFis not a character, it's a special value that indicates that the End Of File indicator is set.
而EOF不一定-1是一个宏,你应该总是用它来测试的EOF指标。而更重要的EOF是不是一个字符,这是一个特殊值,表明Ë次Ø˚F ˚FILE指标集。
Also, getchar()is equivalent to fgetc(stdin).
此外,getchar()相当于fgetc(stdin).
回答by Sourav Ghosh
In linux bash, if you press CTRL+D, it will generate EOF.
在 linux bash 中,如果你按CTRL+ D,它会生成EOF.
In Windows, the equivalent is CTRL+Z
在 Windows 中,相当于CTRL+Z
So, no, if nothing writtento the terminal, that does not generate EOFautomatically. The scanningfunction is in wait state then. So, without having any other inputs, in wait state, if CTRL+Dis pressed, the key press is translated[by the terminal driver] to EOF.Note
所以,不,如果没有写入终端,则不会EOF自动生成。此时扫描功能处于等待状态。所以,无需任何其他的输入,在等待状态中,如果CTRL+D被按压时,键按压被翻译[由终端驾驶员]到EOF。笔记
Usually, once you key in some value and press the ENTERkey, the scannningfunction starts scanning. To feedan input for producing EOF, you need to press CTRL+D.
通常情况下,一旦你在某个值,并且按下键ENTER键,scannning功能开始扫描。为了养活输入用于生产EOF,则需要按CTRL+ D。
Related: Please reaed the wiki entryfor EOF
相关阅读:请reaed的维基条目对EOF
Note: With thanks to Mr Drew for the clarification.
注:感谢德鲁先生的澄清。
回答by axiac
stdinis a stream, data is not available until the user presses some keys. A file on the disk already has (a fixed amount of) content.
stdin是一个流,数据在用户按下某些键之前不可用。磁盘上的文件已经有(固定数量的)内容。
When reading from stdin, if getchar()doesn't wait for the user to input something then the program will always get EOF. That will make it impossible to use stdinas an input file.
从 读取时stdin,如果getchar()不等待用户输入内容,则程序将始终获取EOF. 这将使其无法stdin用作输入文件。
Because getchar()waits for the user to input something there is no way to signal the input completed; that's why the operating systems provide a combination of keys that have this special meaning when they are pressed on the console.
因为getchar()等待用户输入一些东西,所以无法发出输入完成的信号;这就是为什么操作系统提供在控制台上按下时具有这种特殊含义的键组合的原因。
Windows uses CtrlZand Unix-like OSes (including OSX) use CtrlDfor this purpose.
WindowsCtrlZ和类 Unix 操作系统(包括 OSX)CtrlD用于此目的。
回答by Wolf
The file stdinis not always the user typing on the keyboard. If you redirect input to your program, it can be just a normal file.
该文件stdin并不总是用户在键盘上打字。如果您将输入重定向到您的程序,它可能只是一个普通文件。
program.exe <input-from-file.txt
What may be confusing you is that no giving input into a console window does not mark the end of the input. But think it the other way round: how could a user respond so quickly that the program would not terminate before it if the console would not do some buffering for the user? After pressing Enterthe user says this is a line of input. In other words: a program running in a console window always waits for the next input to come.
可能让您感到困惑的是,没有向控制台窗口提供输入并不标志着输入的结束。但反过来想想:如果控制台不为用户做一些缓冲,用户怎么能如此迅速地响应以致程序不会在它之前终止?按下后Enter用户说这是一行输入。换句话说:在控制台窗口中运行的程序总是等待下一个输入的到来。
Most programs define a special phrase to end a console session. You probably know exit.
大多数程序定义一个特殊的短语来结束控制台会话。你可能知道exit。

![C语言 uint8_t 到字符串的转换 [C]](/res/img/loading.gif)