为什么这个 getchar() 循环在输入一个字符后停止?

发布于 2024-08-25 02:50:30 字数 263 浏览 3 评论 0原文

#include <stdio.h>

int main() {
    char read = ' ';

    while ((read = getchar()) != '\n') {
        putchar(read);
    }

    return 0;
}

我的输入是 f (当然后面跟着一个 Enter)。我希望 getchar() 再次请求输入,但程序却被终止。怎么会?我该如何解决这个问题?

#include <stdio.h>

int main() {
    char read = ' ';

    while ((read = getchar()) != '\n') {
        putchar(read);
    }

    return 0;
}

My input is f (followed by an enter, of course). I expect getchar() to ask for input again, but instead the program is terminated. How come? How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

[旋木] 2024-09-01 02:50:30

终端有时可能会有点混乱。您应该将程序更改为:

#include <stdio.h>

int main() {
    int read;
    while ((read = getchar()) != EOF) {
        putchar(read);
    }
    return 0;
}

这将读取直到 getchar 从终端读取 EOF(大多数情况下该宏扩展为 -1)。 getchar 返回一个 int,因此您应该将变量“读”为整数,以便您可以检查 EOF。您可以在 Linux 上使用 ^D 从终端发送 EOF,我认为在 Windows 上使用 ^Z (?)。

解释一下发生了什么。 表达式

(read = getchar()) !='\n'

在您的程序中,只要没有从缓冲区读取“\n”, 就为真。问题是,要获取程序的缓冲区,您必须按与“\n”相对应的回车键。
当您的程序在终端中调用时,会发生以下步骤:

~$\a.out

这将启动您的程序

(empty line)                    

getchar() 进行系统调用以从终端获取输入,并且终端接管

f                   

您在终端中进行的输入。 “f”被写入缓冲区并在终端上回显,您的程序还不知道该字符。

f
f~$                 

你按下回车键。您的缓冲区现在包含“f\n”。 “输入”还向终端发出信号,表明它应该返回到您的程序。你的程序
读取缓冲区并找到 f 并将其放到屏幕上,然后找到 '\n' 并立即停止循环并结束程序。

这将是大多数终端的标准行为。您可以更改此行为,但这取决于您的操作系统。

The Terminal can sometimes be a little bit confusing. You should change your program to:

#include <stdio.h>

int main() {
    int read;
    while ((read = getchar()) != EOF) {
        putchar(read);
    }
    return 0;
}

This will read until getchar reads EOF (most of the time this macro expands to -1) from the terminal. getchar returns an int so you should make your variable 'read' into an integer, so you can check for EOF. You can send an EOF from your terminal on Linux with ^D and I think on windows with ^Z (?).

To explain a little bit what happens. In your program the expression

(read = getchar()) !='\n'

will be true as long as no '\n' is read from the buffer. The problem is, to get the buffer to your program, you have to hit enter which corresponds to '\n'.
The following steps happen when your program is invoked in the terminal:

~$\a.out

this starts your program

(empty line)                    

getchar() made a system call to get an input from the terminal and the terminal takes over

f                   

you made an input in the terminal. The 'f' is written into the buffer and echoed back on the terminal, your program has no idea about the character yet.

f
f~$                 

You hit enter. Your buffer contains now 'f\n'. The 'enter' also signals to the terminal, that it should return to your program. Your progam
reads the buffer and will find the f and put it onto the screen and then find an '\n' and immediatley stop the loop and end your program.

This would be standard behaviour of most terminals. You can change this behaviour, but that would depend on your OS.

我也只是我 2024-09-01 02:50:30

getchar() 返回输入流中的下一个字符。这包括当然也包括换行符等。事实上,除非按“Enter”键,否则您看不到循环中的进度,这是由于您的文件 I/O(在标准输入上工作)不这样做造成的。除非 getchar() 检测到缓冲区末尾的 '\n',否则不会将输入缓冲区移交给 getchar()。您的例程首先阻塞,然后一次性处理两个击键,然后像您指定的那样终止,并在输入流中出现“\n”。事实: getchar() 不会从输入流中删除 '\n' (为什么要这样做?)。

getchar() returns the next character from the input stream. This includes of course also newlines etc. The fact that you don't see progress in your loop unless you press 'Enter' is caused by the fact that your file I/O (working on stdin) doesn't hand over the input buffer to getchar() unless it detects the '\n' at the end of the buffer. Your routine first blocks then handles the two keystrokes in one rush, terminating, like you specified it, with the appearance of '\n' in the input stream. Facit: getchar() will not remove the '\n' from the input stream (why should it?).

疯到世界奔溃 2024-09-01 02:50:30

在 f 之后输入“enter”,即“/n”。
所以循环到此结束。
如果你想取另一个字符,只需在按下回车键后继续将它们一个接一个地放置,循环就会退出。

after f you are putting "enter" which is '/n'.
so the loop ends there.
if you want to take another character just keep on putting them one after the other as soon as enter is pressed the loop exits.

百变从容 2024-09-01 02:50:30

您已经对其进行了编程,以便当您读取 \n(输入)时循环结束,然后返回 0;从退出程序的 main 中。

也许你想要类似的东西

 while ((read = getchar()) != EOF) {
        putchar(read);
    }

You've programmed it so the loop ends when you read a \n (enter), and you then return 0; from main which exits the program.

Perhaps you want something like

 while ((read = getchar()) != EOF) {
        putchar(read);
    }
合久必婚 2024-09-01 02:50:30

nx 终端上,您可以按 Control-D,这将告诉 tty 驱动程序将输入缓冲区返回到读取它的应用程序。这就是为什么新行上的 ^D 结束输入 - 它导致 tty 返回零字节,应用程序将其解释为文件结尾。但它也适用于线路上的任何地方。

On nx terminals you can press Control-D which will tell the tty driver to return the input buffer to the app reading it. That's why ^D on a new line ends input - it causes the tty to return zero bytes, which the app interprets as end-of-file. But it also works anywhere on a line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文