C++ 标准输入流 (stdin) 的文件结尾 (EOF)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3197025/
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(EOF) of Standard input stream (stdin)
提问by Ravi Gupta
Does stdin has any EOF? For e.g - If i start reading from stdin using fread/read then when the following loop will end?
stdin 有 EOF 吗?例如 - 如果我使用 fread/read 从 stdin 开始读取,那么以下循环何时结束?
while ((c = read(0, buffer, BUFSIZ)) > 0) {
.
.
.
}
If the solution of above is no, then is there any way to add EOF in the stdin?
如果上面的解决方案是否定的,那么有没有办法在标准输入中添加EOF?
回答by Archie
Speaking about EOF
in stdin: when you redirect input from file, e.g.:
EOF
在标准输入中谈到:当您从文件重定向输入时,例如:
program <input.txt
the file already has an EOF
, so this is not a problem. In console you can simulate EOF
flag. In UNIX systems it is Ctrl+D, in Windows Ctrl+Z. When you type this in the console, program will behave like it has just reached end of input file.
该文件已经有一个EOF
,所以这不是问题。在控制台中,您可以模拟EOF
标志。在 UNIX 系统中它是Ctrl+ D,在 Windows Ctrl+ 中Z。当您在控制台中键入此内容时,程序的行为就像刚刚到达输入文件的末尾一样。
Edit
编辑
According to a question asked by OP:
根据OP提出的一个问题:
So does it means that stdin don't have EOF and we have to insert them manually using Ctrl+Z or Ctrl+D?
那么这是否意味着标准输入没有 EOF,我们必须使用 Ctrl+Z 或 Ctrl+D 手动插入它们?
Actually -- yes. One may consider stdin (not redirected, but taken from the console) as infinitefile -- no one can tell where does it end. The end of input file, where input ist stdin, must be told literally by Ctrl+Dor Ctrl+Z.
其实,是。人们可能会将 stdin(未重定向,而是从控制台获取)视为无限文件——没有人知道它在哪里结束。输入文件的结尾,其中 input 是 stdin,必须用Ctrl+D或Ctrl+字面意思告诉Z。
回答by sui
I have never programmed C in windows so I can't tell you but in bash, the program will get an EOF when you type end of data (Ctrl+D)
我从来没有在 Windows 中编写过 C,所以我不能告诉你,但是在 bash 中,当你输入数据结束时,程序会得到一个 EOF (Ctrl+D)
回答by James Curran
while ((c = read(0, buffer, BUFSIZ)) > 0) {
while ((c = read(0, buffer, BUFSIZ)) > 0) {
You don't say the type of c
but using that name implies that it's a char
. Note that the EOF value for iosteams is an (int) -1
. Storing that into an unsigned char will get you a value of 255 which will not match EOF.
你没有说的类型,c
但使用这个名字意味着它是一个char
. 请注意,iosteams 的 EOF 值是一个(int) -1
. 将其存储到无符号字符中将使您获得与 EOF 不匹配的 255 值。
回答by user1364455
First getchar() is really getc(stdin) so getc(FILE) you can read more from that. getchar() gets you last unprocessed character from input stream or stdin pressing enter is '\n'. if the stdin is empty getchar forces a pause to get input. Say in a program I call getchar() at first the stdin is empty so it pauses for input. If I enter ab'\n' the first getchar() will get 97 the ascii of 'a'. the next time i call getchar() i will get b , then again getchar() will have '\n'.
首先 getchar() 实际上是 getc(stdin) 所以 getc(FILE) 你可以从中阅读更多。getchar() 从输入流或标准输入中获取最后一个未处理的字符,按 Enter 是 '\n'。如果标准输入为空 getchar 强制暂停以获取输入。说在一个程序中,我首先调用 getchar() 标准输入是空的,所以它暂停输入。如果我输入 ab'\n',第一个 getchar() 将得到 97 'a' 的 ascii。下次我调用 getchar() 时,我将得到 b ,然后 getchar() 将再次得到 '\n'。
To prove this write this code.
为了证明这一点,请编写此代码。
int choice;
do
{
cout << "Enter input:" ;
choice = getchar();
cout << "Last getchar(): " << choice << ":" << (char) choice ;
if( choice == '0' || choice == EOF)
{
cout << "Exited loop" << endl; // EOF is ctrl+z in windows
break;
}
}while(true);
I do believe stdin is a global so until getchar() or similar function gets called it to clear the stream the characters remain there which can cause bugs later if you use getchar() elsewhere. As people have mentioned you can use gets(char[]) which puts all characters until newline into the character array. The problem with this is you need a char[] larger than input or you will get errors. The nice thing is gets(char[]) does clear stdin so you can make a dumby buffer to clear stdin or process it.
我确实相信 stdin 是全局的,因此在调用 getchar() 或类似函数以清除流之前,字符会保留在那里,如果您在其他地方使用 getchar(),则稍后可能会导致错误。正如人们所提到的,您可以使用 gets(char[]) 将所有字符直到换行符放入字符数组中。问题是你需要一个比输入大的 char[] 否则你会得到错误。好消息是 gets(char[]) 确实清除了标准输入,因此您可以制作一个笨拙的缓冲区来清除标准输入或处理它。
I hope this is informative.
我希望这是信息性的。
回答by user1364455
The way to test for EOF is to check the return value of fread, and then use feof:
测试EOF的方法是检查fread的返回值,然后使用feof:
while( fread( ... ) ) { // loop so long as fread does not return zero
// do something
}
if ( feof( stdin ) ) {
// read failed because of EOF
}
else {
// some other error
}
回答by Puppy
Your solution is tagged C++, so here's some C++.
您的解决方案被标记为 C++,所以这里有一些 C++。
std::string lols;
while(!(std::cin >> lols).eof()) { // This loop will end when EOF is reached
// Process string
}