C语言中标准输入的问题 [英] Issues with standard input in C

查看:73
本文介绍了C语言中标准输入的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C编写一个简单的程序来读取输入.然后显示使用的字符数.

I'm making a simple program in C that reads an input. It then displays the number of characters used.

我第一次尝试:

#include <stdio.h>

int main(int argc, char** argv) {
    int currentChar;
    int charCount = 0;

    while((currentChar = getchar()) != EOF) {
        charCount++;
    }

    printf("Display char count? [y/n]");
    int response = getchar();

    if(response == 'y' || response == 'Y')
        printf("Count: %d\n",charCount);
}

发生了什么事

我要输入几行并以 ^ D 结尾(我在Mac上是).该程序不会在 int response = getchar(); 处等待.我在网上发现这是因为输入流中仍然有内容.

I would enter some lines and end it with ^D (I'm on Mac). The program would not wait at int response = getchar();. I found online that this is because there is still content left in the input stream.

我的第一个问题是那将是什么内容?在按 ^ D 输入 EOF 后,我什么也没输入,当我尝试打印流中剩余的任何内容时,它将打印出?.

My first question is what content would that be? I don't enter anything after pressing ^D to input EOF and when I tried to print anything left in the stream, it would print a ?.

我接下来要尝试的内容:

假设输入流中还剩下字符,我做了一个清除输入缓冲区的函数:

Assuming there were characters left in the input stream, I made a function to clear the input buffer:

void clearInputBuffer() {
    while(getchar() != '\n') {};
}

我在while循环之后立即调用了该函数:

I called the function right after the while loop:

while((currentChar = getchar()) != EOF) {
    charCount++;
}
clearInputBuffer();

现在,我假设按 ^ D 后是否还有任何内容,它将清除到下一个 \ n .

Now I would assume if there is anything left after pressing ^D, it would be cleared up to the next \n.

但是,我无法停止输入请求.当我按 ^ D 时,而不是将 EOF 发送到 currentChar ,而是在终端上显示了 ^ D .

But instead, I can't stop the input request. When I press ^D, rather than sending EOF to currentChar, a ^D is shown on the terminal.

我知道在线上可能有解决方案,但是由于我不确定我的问题到底是什么,所以我真的不知道要寻找什么.

I know there is a probably a solution to this online, but since I'm not sure what exactly my problem is, I don't really know what to look for.

为什么会这样?有人还能解释一下该程序和终端幕后发生的一切吗?

Why is this happening? Can someone also explain exactly what is going on behind the scenes of this program and the Terminal?

推荐答案

Ctrl + D 在Unix上有点奇怪-它实际上不是EOF字符.而是向外壳发出一个信号,指示应该关闭 stdin .结果,该行为可能有些不直观.连续两个 Ctrl + D 或一个 Return ,后跟一个 Ctrl + D ,将为您提供所需的行为.我用以下代码对其进行了测试:

Ctrl+D is a bit weird on Unix -- it's not actually an EOF character. Rather, it's a signal to the shell that stdin should be closed. As a result, the behavior can be somewhat unintuitive. Two Ctrl+Ds in a row, or a Return followed by a Ctrl+D, will give you the behavior you're looking for. I tested it with this code:

#include <stdio.h>

int main(void) {
    size_t charcount = 0;

    while (getchar() != EOF)
        charcount++;

    printf("Characters: %zu\n", charcount);

    return 0;
}

经过修改,包含了 chux 的格式字符建议.

Edited to include chux's format character suggestion.

这篇关于C语言中标准输入的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆