在 linux ctrl + d(EOF) 行为中三种不同情况下

In linux ctrl + d(EOF) behavior in three different situation

提问人:Beywal 提问时间:9/3/2020 更新时间:9/22/2020 访问量:84

问:

//first example
void readInWBW()
{
    int ch; 
    while((ch=getchar()) != EOF)
        putchar(ch);
}

当我输入“qweCTRL+D”时,第一次输入 ctrl+z 只需刷新缓冲区,然后重新输入 “ctrl+d”,所以就像“qweCTRL+DCTRL+D”,然后EOF工作,程序终止。 结果是

$ ./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 是:

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”不再响应,即使我输入“#”,它仍然没有终止。 结果是:

$ ./a.out
qweqwe
#
#
^C
$

我不明白为什么在 example2 和 example3 中具有无限循环并且无法终止程序。谁能解释一下,谢谢。

C Linux IO EOF

评论

5赞 interjay 9/3/2020
第二个和第三个示例不检查 EOF。

答:

0赞 klutt 9/5/2020 #1

让我们关注你的第三个例子。

void test()
{
    char ch;
    while((ch = getchar()) != '#')
        putchar(ch); 
}

请记住,输入 C-d 意味着“文件结束”,因此程序停止响应输入是很自然的。你说过没有更多的投入。这就是EOF的意思。

因此,当您输入 C-d 时,将发生的情况是将不断读取 EOF。您在 C-d 之后键入的任何内容都不会进入输入流。因此,为什么你进入无限循环的简单答案是一个简单的事实,即在读取一次后总是会读取EOF的事实。ch = getchar()EOF != '#'getchar()

这里可能值得一提的是,您正在使用缓冲输入。所以会简单地等到有东西在.当您输入几个字符时,它们不会被刷新,直到您按回车键或 C-d。getchar()stdinstdin

另外,请记住,这不适合 .该函数返回一个,该数字要么是适合 a 中的数字,要么是 。该变量应声明为 int 并正确检查 .EOFchargetcharintcharEOFchEOF

可以 重置 ,尽管我不确定是否可以以便携式方式进行。但这里有一个问题:如何在 Ctrl+D 之后重新启动 stdin?stdin

评论

0赞 Beywal 9/5/2020
谢谢,这很有帮助,我尝试在第三个示例中在“ while((ch = getchar()) != '#') putchar(ch); ”之后插入printf(“1”),正如您解释的那样,它也进入了无限循环。