C getchar 与 scanf

发布于 2024-09-18 09:17:39 字数 657 浏览 11 评论 0原文

我对我正在研究的函数中发现的一段代码感到困惑:

char GetCommand( void )
{
    char command;

    do {
        printf( "Enter command (q=quit, n=new, l=list):  " );
        scanf( "%c", &command );
        Flush();
    }
    while ( (command != 'q') && (command != 'n')
           && (command != 'l') );

    printf( "\n----------\n" );
    return( command );
}

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

我不太明白的是 Flush() 函数的用法。我的意思是,我正在阅读的书解释说,它可以防止用户输入多个字符,然后在提示他们第二次输入时读取该字符。

我不明白的是 Flush() 如何防止这种情况发生。它没有任何作用。它只是一个 while 命令。 (虽然这是真的……什么??????)没有意义。

I am confused by a piece of code found in a function I am studying:

char GetCommand( void )
{
    char command;

    do {
        printf( "Enter command (q=quit, n=new, l=list):  " );
        scanf( "%c", &command );
        Flush();
    }
    while ( (command != 'q') && (command != 'n')
           && (command != 'l') );

    printf( "\n----------\n" );
    return( command );
}

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

What I don't quite understand here is the usage of the Flush() function. I mean, the book I am reading explains it by saying that it prevents the user from inputting more than a single character and then having that character read when they are prompted for input the 2nd time.

What I don't understand is how Flush() is preventing this from happening. It doesn't DO anything. All it is is a while command. (While this is true......what?????) Doesn't make sense.

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

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

发布评论

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

评论(3

痴者 2024-09-25 09:17:39

getchar() 具有从输入缓冲区中删除下一个字符的副作用。 Flush 中的循环读取并丢弃字符,直到换行符 \n 结束该行。

由于 scanf 被告知读取一个且仅一个字符 (%c),这会导致忽略该输入行上的所有其他内容。

如果将 scanf 替换为,可能会更清楚

command = getchar();

,但它实际上是一个通常不好的例子,因为它不能很好地处理文件结束。

一般来说,最好忘记 scanffgetssscanf 工作得更好,因为一个负责获取输入,另一个负责解析它。 scanf(和fscanf)尝试一次执行太多作业。

getchar() has the side effect of removing the next character from the input buffer. The loop in Flush reads and discards characters until - and including - the newline \n ending the line.

Since the scanf is told to read one and only one character (%c) this has the effect of ignoring everything else on that input line.

It would probably be more clear if the scanf was replace with

command = getchar();

but it's actually a generally bad example as it does not handle End Of File well.

In general scanf is best forgotten; fgets and sscanf work much better as one is responsible for getting the input and the other for parsing it. scanf (and fscanf) try to do too many jobs at once.

握住我的手 2024-09-25 09:17:39

getchar 从标准输入读取一个字符。如果将其放入 while 循环中,它将继续一次读取一个字符,直到条件为 false。

Flush 函数正在执行读取操作,直到遇到换行符 (\n)。这是当用户按下回车键时产生的字符。

因此,您给出的代码将读取一个字符(我不清楚为什么它使用 scanf 来代替简单的 getchar,这样会更快),然后丢弃其余的输入,直到用户按下回车键。

如果您要向此程序提供 foobar,它将在 Flush 函数中获取 f 并丢弃 oobar 。如果不调用 flushf 可以转到一个 scanf,第二个 scanf 将获取第一个 scanf > o 。

getchar reads one character from standard input. If you put it in a while loop, it will continue to read one character at a time until the condition is false.

What the Flush function is doing is reading until it encounters a newline (\n). This is the character produced when the user hits the enter key.

So, the code you gave will read one character (I'm unclear on why it uses scanf for this instead of simply getchar, which would be faster), and then discards the rest of the input until the user hits enter.

If you were to feed this program foobar, it would take the f and discard the oobar in the Flush function. Without calling flush, the f could go to one scanf, and the second scanf would get the first o.

苍白女子 2024-09-25 09:17:39

当您输入字符并按 Enter 键时,按 Enter 键会生成换行符,并将其保留在缓冲区中。这是有问题的,因为它将等到您下次需要用户输入时才将其用于该输入。 Flush 用于从输入缓冲区中刷新换行符,这样就不会出现这个问题。 Flush实际上在读取时使用了输入缓冲区中的换行符,并且将其丢弃,因此它不再位于缓冲区中。

When you enter your character and press Enter, a newline character is generated by you pressing the Enter key and it remains in the buffer. This is problematic because it will wait around until the next time you require user input and it will be used for that input. Flush is used to flush the newline character from the input buffer so you don't have that problem. Flush actually uses the newline in the input buffer when it reads it and it is discarded, so it is no longer in the buffer.

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