C语言 while ((c = getchar()) != EOF) 不终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18565663/
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
while ((c = getchar()) != EOF) Not terminating
提问by user2738586
I've been reading "The C Programming Language" and I got to this part of inputs and outputs.
我一直在阅读“C 程序设计语言”,并且我了解了输入和输出的这一部分。
I've read other threads saying that the console doesn't recognize enter as EOF. So that I should use CTRL+ Zin Windows or CTRL+ Din Unix (neither of those is working for me).
我读过其他线程说控制台无法识别输入为EOF. 所以,我应该使用CTRL+Z在Windows或CTRL+D在UNIX(这些都不是对我来说有效)。
I also read other people asking the same saying they could make it work, the problem in their codes was syntax not the program not terminating.
我也读过其他人也问过同样的问题,他们说他们可以让它工作,他们代码中的问题是语法而不是程序没有终止。
Is there another solution?
还有其他解决方案吗?
This is the code:
这是代码:
#include <stdio.h>
main()
{
int nb, nl, nt, c;
nb = 0;
nl = 0;
nt = 0;
while ((c = getchar()) != '\n'){
if (c == ' ')
++nb;
else if (c == '\n')
++nl;
else if (c == '\t')
++nt;
}
printf("Input has %d blanks, %d tabs, and %d newlines\n", nb, nt, nl);
}
Edit: The \nwas supposed to be an EOF, I was messing around before I posted and I forgot I changed it :P
编辑:这\n应该是一个 EOF,我在发帖之前搞砸了,我忘了我改变了它:P
It doesn't work with EOF neither, I just skipped that one.
它也不适用于 EOF,我只是跳过了那个。
回答by P0W
while ((c = getchar()) !=EOF) {
}
Then use Ctrl+Zor F6on Windows
然后在 Windows 上使用Ctrl+Z或F6
Following will wait for either a \nor EOF, which comes first
以下将等待 a\n或EOF,首先出现
while((c = getchar()) != '\n' && c != EOF){
}
回答by P0W
On Windows, you type Ctrl-Zon a line by itself (no spaces or anything) and type the Returnafter that. On Windows, you could technically skip the EOF indicator and keep reading characters, though this doesn't apply to other operating systems because EOF actually means EOF.
在 Windows 上,您可以单独输入Ctrl-Z一行(没有空格或任何内容),然后输入Return。在 Windows 上,从技术上讲,您可以跳过 EOF 指示符并继续读取字符,但这不适用于其他操作系统,因为 EOF 实际上意味着 EOF。
回答by ANjaNA
while ((c = getchar()) != '\n'){
if (c == ' ')
++nb;
else if (c == '\n')
++nl;
else if (c == '\t')
++nt;
}
According to the condition in your while loop, you will not be able to count number of new lines because you will stop the whileloop when the user inputs a new line character ('\n')
根据 while 循环中的条件,您将无法计算新行的数量,因为while当用户输入新行字符 ('\n') 时,您将停止循环
Other than that, counting blanks and tabs just works fine.
除此之外,计数空白和制表符就很好用。
CTRL+Zwill be recognized as a EOF in windows. But in order to recognize it in your program, use condition in while loop as ((c = getchar()) != EOF).
Now when the user presses the key combination: CTRL+Z, It will input to console as EOF, and the program should recognize it as a character input.
CTRL+Z将在 Windows 中被识别为 EOF。但是为了在您的程序中识别它,请在 while 循环中使用 condition as ((c = getchar()) != EOF)。现在,当用户按下组合键:CTRL+ 时Z,它将作为 EOF 输入到控制台,程序应该将其识别为字符输入。
Doing this will allow you to count number of lines in the input
这样做将允许您计算输入中的行数
So, my suggestion is:
所以,我的建议是:
while ((c = getchar()) != EOF){
if (c == ' ')
++nb;
else if (c == '\n')
++nl;
else if (c == '\t')
++nt;
}
回答by smac89
If you are on unix system:
如果您使用的是 unix 系统:
The only way to simulate EOFvia keyboard while feeding input to the program manually is to press CTRL+D
在手动向程序输入输入时通过键盘模拟EOF的唯一方法是按CTRL+D
Here are a couple methods of feeding input to your program and be able to signal EOF at the end of the input:
以下是向程序提供输入并能够在输入结束时发出 EOF 信号的几种方法:
Make use of the here stringformat to feed a string representation of a file to the program.
./myprog <<< "Here is a string to work with"
Alternatively, you can also use input redirection to feed a file containing the input to the program.
./myprog < input.file
使用here 字符串格式将文件的字符串表示形式提供给程序。
./myprog <<< "这是一个要处理的字符串"
或者,您还可以使用输入重定向将包含输入的文件提供给程序。
./myprog < input.file
Any of the above listed methods will work with the following code:
上面列出的任何方法都适用于以下代码:
#include <stdio.h>
#ifndef EOF
#define EOF (-1)
#endif
int main(void)
{
int nb, nl, nt, c;
nb = 0;
nl = 0;
nt = 0;
while ((c = getchar()) != EOF){
if (c == ' ')
++nb;
else if (c == '\n')
++nl;
else if (c == '\t')
++nt;
}
printf("Input has %d blanks, %d tabs, and %d newlines\n", nb, nt, nl);
return 0;
}
- Not to leave windows out of the game. To simulate EOF on keyboard on windows, use CTRL+Zkey combination
- 不要将窗口排除在游戏之外。要在 Windows 上的键盘上模拟 EOF,请使用CTRL+Z组合键
Not sure if the here-string format for unix is available for windows, but input redirection should be similar
不确定 unix 的 here-string 格式是否可用于 windows,但输入重定向应该类似
回答by Darshit Mulani
/* This program will calculate the number of blanks, tabs and new line in a text stream */
#include <stdio.h>
main ()
{
int c,nl = 0, blank = 0, tab = 0; //Newline, blanks and tabs.
while ((c = getchar()) != EOF) {
if (c == '\n')
++nl;
else if (c == '\t')
++tab;
else if (c == ' ')
++blank;
}
printf("Tabs = %d\nBlanks = %d\nNewLine = %d",tab, blank, nl);
}
I wrote this following code and it works properly on Ubuntu. As it is similar to what you wrote, I tried the code and Ctrl-D is working properly in UNIX.
我写了以下代码,它在 Ubuntu 上正常工作。由于它与您编写的内容相似,因此我尝试了代码并且 Ctrl-D 在 UNIX 中正常工作。
I tested the following code and have realized that if we input \n in text stream it will not increase the counter of the new line, same goes for a \t tab. Only pressing enter for new line and pressing tab for tab is counted by the counter this is a point to be noted.
我测试了以下代码并意识到如果我们在文本流中输入 \n 它不会增加新行的计数器,\t 选项卡也是如此。只有按回车换行和按制表符按制表符才会被计数器计数,这是需要注意的一点。
This is happening because pressing enter actually puts a newline character which is a single character whereas entering \n is treated as differently and they are actually two characters.
发生这种情况是因为按 Enter 实际上会放置一个换行符,它是单个字符,而输入 \n 被视为不同,它们实际上是两个字符。
I thought this will add value to this question so I explained this thing too. Thanks.
我认为这会增加这个问题的价值,所以我也解释了这件事。谢谢。
回答by user3647605
First of all press Ctrl+ Zwhich will print ^Zthen press Enterto go to EOF..
首先按Ctrl+Z这将打印 ^Z然后按 Enter转到 EOF ..
回答by Eric Z
Change the line
换线
// Buggy, you want calculate the # of '\n' in loop body,
// so logically you shouldn't use it as a stop condition.
while ((c = getchar()) != '\n')
to
到
while ((c = getchar()) != EOF)
And try press Ctrl + Cin your console window on Windows. It works on my platform which is running Win7.
并尝试Ctrl + C在 Windows 上的控制台窗口中按下。它适用于我运行 Win7 的平台。


回答by Sohan Joshi
To recognize EOF in Turbo C++, in the terminal window press Ctrl+z; this will act as EOF in the program and then terminate the program....with the condition:
要在 Turbo C++ 中识别 EOF,请在终端窗口中按Ctrl+ z; 这将在程序中充当 EOF,然后终止程序....条件是:
while ((c = getchar()) != EOF)

