问题描述
//first example
void readInWBW()
{
int ch;
while((ch=getchar()) != EOF)
putchar(ch);
}
当我输入 qweCTRL + D时,第一次输入ctrl + z刷新缓冲区,然后重新输入
ctrl + d就像 qweCTRL + DCTRL + D,然后EOF工作,程序终止。
结果是
when I input "qweCTRL+D", the first time Where I input ctrl+z just flush the buffer, and I re-input"ctrl+d" so it's like "qweCTRL+DCTRL+D", then the EOF work, program terminate.result is
$ ./a.out
qweqwe$
//second example
void guess()
{
int guess = 1;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right and with");
printf("\nan n if it is wrong.\n");
printf("Uh...is your number %d?\n", guess);
while (getchar() != 'y'){ //<---------------------------
printf("Well, then, is it %d?\n", ++guess); //<----------
sleep(1);
}
printf("I knew I could do it!\n");
}
在此示例中,我输入 qweCTRL + D,它将显示三倍好吧……,但如果我再次输入CTRL + D,程序将进入无限循环。
reuslt是:
In this example, I input "qweCTRL+D", it will shows three times "Well, then...", but if I input CTRL+D again, the program will get into infinite loop.reuslt is:
Pick an integer from 1 to 100. I will try to guess it.
Respond with a y if my guess is right and with
an n if it is wrong.
Uh...is your number 1?
qweWell, then, is it 2? //<--------------------------
Well, then, is it 3?
Well, then, is it 4?
Well, then, is it 5?
Well, then, is it 6?
Well, then, is it 7?
Well, then, is it 8?
Well, then, is it 9?
Well, then, is it 10?
Well, then, is it 11?
^C
//third example
void test()
{
char ch;
while((ch = getchar()) != '#')
putchar(ch);
}
我尝试输入 qweCTRL + D;与其他示例类似,但是在刷新缓冲区之后, CTRL + D不再响应,即使我输入#,它也不会终止。
结果是:
I tried to input "qweCTRL+D" like other example, but after flush buffer, "CTRL+D" not response anymore, even if I input "#", it still not terminate.result is:
$ ./a.out
qweqwe
#
#
^C
$
我不明白为什么example2和example3中有无限循环并且可以t终止程序。谁能解释它,谢谢。
I don't understand why in example2 and example3 have infinite loop and can't terminate the program. can anyone explains it, thank you.
推荐答案
让我们关注第三个示例。
Let's focus on your third example.
void test()
{
char ch;
while((ch = getchar()) != '#')
putchar(ch);
}
请记住,输入Cd表示文件结束,因此程序停止自然是很自然的响应输入。您已经说过没有更多输入。这就是EOF的意思。
Remember that inputting C-d means "end of file", so it's very natural that the program stops responding to input. You have said that there is no more input. That's what EOF means.
因此,当您输入Cd时,将发生 ch = getchar()
不断读取EOF的情况。 。在C-d之后键入的任何内容都不会进入输入流。因此,为什么要进入无限循环的简单答案是 EOF!='#'
与 getchar()的事实相结合的简单事实
一次读取EOF总是会读取它。
So when you have inputted C-d, what will happen is that ch = getchar()
will constantly read EOF. Whatever you type after C-d will not go into the input stream. So the simple answer to why you enter an infinite loop is the simple fact that EOF != '#'
in combination with the fact that getchar()
always will read EOF after it has read it once.
在这里可能要提到的另一件事是您正在使用缓冲输入。因此, getchar()
只会等到 stdin
中有东西。当您输入几个字符时,除非您按Enter或Cd,否则它们不会刷新到 stdin
。
Another thing that might be good to mention here is that you are using buffered input. So getchar()
will simply wait until there is something in stdin
. When you enter a few characters, they will not be flushed to stdin
until you press enter OR C-d.
此外,请记住 EOF
不适合放在 char
中。函数 getchar
返回 int
,该数字可以是 char的数字
或 EOF
。变量 ch
应该声明为int并正确检查 EOF
。
Also, remember that EOF
does not fit in a char
. The function getchar
returns an int
which is either a number that fits in a char
or EOF
. The variable ch
should be declared as an int and properly checked for EOF
.
可以重置 stdin
,尽管我不确定是否可以通过便携式方式进行重置。但这是一个问题:
It is possible to reset stdin
, although I'm not sure if it's possible in a portable way. But here is a question about that: How to restart stdin after Ctrl+D?
这篇关于在linux ctrl + d(EOF)中三种情况下的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!