[英]while ((c = getchar()) != EOF) Not terminating
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
.我读过其他线程说控制台无法将输入识别为
EOF
。 So that I should use CTRL + Z in Windows or CTRL + D in Unix (neither of those is working for me).所以我应该在 Windows 中使用CTRL + Z或在 Unix 中使用CTRL + D (这些都不适合我)。
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 \n
was 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
,我只是跳过了那个。
while ((c = getchar()) !=EOF) {
}
Then use Ctrl+Z or F6 on Windows然后在 Windows 上使用Ctrl+Z或F6
Following will wait for either a \\n
or EOF
, which comes first以下将等待
\\n
或EOF
,首先出现
while((c = getchar()) != '\n' && c != EOF){
}
On Windows, you type Ctrl-Z on a line by itself (no spaces or anything) and type the Return after that.在 Windows 上,您可以在一行上单独键入Ctrl-Z (没有空格或任何内容),然后在此之后键入Return 。 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 上,从技术上讲,您可以跳过 EOF 指示符并继续读取字符,但这不适用于其他操作系统,因为 EOF 实际上意味着 EOF。
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 while
loop when the user inputs a new line character ('\\n')根据 while 循环中的条件,您将无法计算新行的数量,因为当用户输入新行字符 ('\\n') 时,您将停止
while
循环
Other than that, counting blanks and tabs just works fine.除此之外,计数空白和制表符就很好用。
CTRL + Z will be recognized as a EOF in windows. CTRL + Z将在 Windows 中被识别为 EOF。 But in order to recognize it in your program, use condition in while loop as
((c = getchar()) != EOF)
.但是为了在您的程序中识别它,请在 while 循环中使用条件作为
((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 时,它将作为 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;
}
If you are on unix system:如果您使用的是 unix 系统:
The only way to simulate EOF via 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 信号的几种方法:
./myprog <<< "Here is a string to work with"
./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 sure if the here-string format for unix is available for windows, but input redirection should be similar不确定 unix 的 here-string 格式是否可用于 windows,但输入重定向应该类似
/* 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.我写了以下代码,它在 Ubuntu 上正常工作。 As it is similar to what you wrote, I tried the code and Ctrl-D is working properly in UNIX.
由于它与您编写的内容相似,因此我尝试了代码并且 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.我测试了以下代码并意识到如果我们在文本流中输入 \\n 它不会增加新行的计数器,\\t 选项卡也是如此。 Only pressing enter for new line and pressing tab for tab is counted by the counter this is a point to be noted.
只有按回车换行和按制表符按制表符才会被计数器计数,这是需要注意的一点。
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.
谢谢。
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 + C
in your console window on Windows.并尝试在 Windows 的控制台窗口中按
Ctrl + C
It works on my platform which is running Win7.它适用于我运行 Win7 的平台。
首先按Ctrl + Z这将打印^Z然后按Enter转到 EOF ..
I had the same problem, so I check the value of ctrl+D by debugging my code and I found that the value of ctrl+D is 255 and I replaced the EOF with 255, and thats worked for me, if its not working for you try to figure out the value of ctrl + D in Unix.我有同样的问题,所以我通过调试我的代码检查了 ctrl+D 的值,我发现 ctrl+D 的值是 255,我用 255 替换了 EOF,这对我有用,如果它不工作您尝试找出 Unix 中 ctrl + D 的值。
#include <stdio.h>
main()
{
int nb, nl, nt, c;
nb = 0;
nl = 0;
nt = 0;
while ((c = getchar()) != 255) {
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);
}
To recognize EOF in Turbo C++, in the terminal window press Ctrl + z ;要在 Turbo C++ 中识别 EOF,请在终端窗口中按Ctrl + z ; this will act as EOF in the program and then terminate the program....with the condition:
这将在程序中充当 EOF,然后终止程序....条件是:
while ((c = getchar()) != EOF)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.