如何清除C中的输入缓冲区? [英] How to clear input buffer in C?

查看:37
本文介绍了如何清除C中的输入缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下程序:

int main(int argc, char *argv[])
{
  char ch1, ch2;
  printf("Input the first character:"); // Line 1
  scanf("%c", &ch1); 
  printf("Input the second character:"); // Line 2
  ch2 = getchar();

  printf("ch1=%c, ASCII code = %d
", ch1, ch1);
  printf("ch2=%c, ASCII code = %d
", ch2, ch2);

  system("PAUSE");  
  return 0;
}

正如上面代码的作者所解释的:程序将无法正常运行,因为在第 1 行,当用户按下 Enter 键时,它会在输入缓冲区中留下 2 个字符:Enter 键(ASCII 码 13) (ASCII 码)10).因此,在第 2 行,它将读取 并且不会等待用户输入字符.

As the author of the above code have explained: The program will not work properly because at Line 1, when the user presses Enter, it will leave in the input buffer 2 character: Enter key (ASCII code 13) and (ASCII code 10). Therefore, at Line 2, it will read the and will not wait for the user to enter a character.

好的,我知道了.但我的第一个问题是:为什么第二个 getchar() (ch2 = getchar();) 不读取 Enter 键 (13), 而不是 字符?

OK, I got this. But my first question is: Why the second getchar() (ch2 = getchar();) does not read the Enter key (13), rather than character?

接下来,作者提出了两种解决此类问题的方法:

Next, the author proposed 2 ways to solve such probrems:

  1. 使用fflush()

写一个这样的函数:

void
clear (void)
{    
  while ( getchar() != '
' );
}

这段代码确实有效.但我无法解释它是如何工作的?因为在while语句中,我们使用了getchar() != ' ',这意味着读取除' '之外的任何单个字符?如果是这样,输入缓冲区中仍然保留' '字符?

This code worked actually. But I cannot explain myself how it works? Because in the while statement, we use getchar() != ' ', that means read any single character except ' '? if so, in the input buffer still remains the ' ' character?

推荐答案

程序将无法正常运行,因为在第 1 行,当用户按下 Enter 键时,它会在输入缓冲区中留下 2 个字符:Enter 键(ASCII 代码 13)和 (ASCII 代码 10).因此,在第 2 行,它将读取 并且不会等待用户输入字符.

The program will not work properly because at Line 1, when the user presses Enter, it will leave in the input buffer 2 character: Enter key (ASCII code 13) and (ASCII code 10). Therefore, at Line 2, it will read the and will not wait for the user to enter a character.

您在第 2 行看到的行为是正确的,但这并不是正确的解释.对于文本模式流,您的平台使用什么换行符并不重要(回车 (0x0D) + 换行符 (0x0A)、裸 CR 或裸 LF).C 运行时库将为您处理这些:您的程序将只看到 ' ' 换行符.

The behavior you see at line 2 is correct, but that's not quite the correct explanation. With text-mode streams, it doesn't matter what line-endings your platform uses (whether carriage return (0x0D) + linefeed (0x0A), a bare CR, or a bare LF). The C runtime library will take care of that for you: your program will see just ' ' for newlines.

如果您输入一个字符并按下回车键,那么该输入字符将被第 1 行读取,然后 ' ' 将被第 2 行读取.参见 我正在使用 scanf %c 来读取 Y/N 响应,但后来的输入被跳过. 来自 comp.lang.c 常见问题解答.

If you typed a character and pressed enter, then that input character would be read by line 1, and then ' ' would be read by line 2. See I'm using scanf %c to read a Y/N response, but later input gets skipped. from the comp.lang.c FAQ.

至于建议的解决方案,请参阅(再次来自 comp.lang.c 常见问题解答):

As for the proposed solutions, see (again from the comp.lang.c FAQ):

  • How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work?
  • If fflush won't work, what can I use to flush input?

基本上说明唯一可移植的方法是:

which basically state that the only portable approach is to do:

int c;
while ((c = getchar()) != '
' && c != EOF) { }

您的 getchar() != ' ' 循环有效,因为一旦您调用 getchar(),返回的字符已从输入流中删除.

Your getchar() != ' ' loop works because once you call getchar(), the returned character already has been removed from the input stream.

另外,我觉得有必要劝阻你完全使用 scanf:为什么大家都说不要用scanf?我应该用什么代替?

Also, I feel obligated to discourage you from using scanf entirely: Why does everyone say not to use scanf? What should I use instead?

这篇关于如何清除C中的输入缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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