普通 C 标准输入缓冲区垃圾和换行符 [英] plain-C stdin buffer garbage and newline-eaters

查看:40
本文介绍了普通 C 标准输入缓冲区垃圾和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于标准输入缓冲区内容检查的问题.

I have a question concerning stdin buffer content inspection.

这行广受好评的代码:

int c; while((c = getchar()) != '\n' && c != EOF);

有效地处理丢弃标准输入缓冲区垃圾,以防发现垃圾.如果缓冲区为空,程序执行就不会越过它.

deals efficiently with discarding stdin-buffer garbage, in case there is a garbage found. In case the buffer is empty, the program execution wouldn't go past it.

有没有办法检查标准输入缓冲区中是否有垃圾(无论是由于用户错误、提前输入或任何原因造成的),并且仅在万一情况下才从上面执行fflush-replacement line"有没有发现垃圾?

Is there a way of checking if there is garbage in the stdin-buffer at all (no matter if it's there by user error, typeahead or whichever reason), and executing the "fflush-replacement line" from above only in case there is a garbage found?

我更愿意以编程方式将它全部保留在标准 C 语言中,无需使用特殊的解析工具,没有 yacc、bison、python、ruby、shell 脚本等,没有 Windows API,请.

I'd prefer to keep it programmatically all in plain-UNIX-flavor-of standard C, without having to use special parsing tools, no yacc, bison, python, ruby, shell scripts etc., no Windows API, please.

提前致谢!

更新:

我希望这个例子能更多地说明我的问题:

I hope this example tells a bit more of my question:

//...
//this line should make sure stdin buffer is free from accidentally typed content
int c; while (( c = getchar()) != '\n' && c != EOF);

//this line won't show in case buffer is already clean
printf("Please enter an arbitrary number of float or symbolic values:\n");

//this line should read the real input user is being asked for
char* p = fgets(text, TEXT_SIZE, stdin);
if(p != NULL)
parse_and_process(text);
//...

问题发生在没有意外输入的情况下.这里的垃圾"被认为是在 printf( ) 提示出现时可能留在缓冲区中的任何东西.如果缓冲区已经干净,有没有办法绕过第一行?

The problem happens when there is no accidental input. The "garbage" is here considered anything that may stay in the buffer at the moment printf( ) prompt would appear. Is there a way of getting around the first line in case the buffer is already clean?

推荐答案

Edit 回复 OP 评论.

Edit to respond to OP comment.

您需要指定什么是垃圾.只有这样,您(或其他任何人)才能确定哪些输入是有效的.

You need to specify what is garbage. Only then can you (or anyone else) determine what input is valid.

一旦您确定了这一点,您就可以编写一个例程,安静地使用 垃圾 类别的用户输入,直到检测到一些非垃圾输入.

Once you have determined that, you can write a routine that quietly consumes user input with the category of garbage until some non-garbage input is detected.

conio.h 在 Windows(非 Unix)上的 C 编程中使用的头文件包含用于控制台输入/输出的函数.conio.h 最常用的一些函数是clrscrgetchgetchekbhit 等.

The conio.h header used in C programming on Windows (not Unix) contains functions for console input/output. Some of the most commonly used functions of conio.h are clrscr, getch, getche, kbhit etc.

以下是来自此处的示例,说明了如何您可能会运行一个消耗输入的循环,直到 non-garbage 出现在键盘上......(在这种情况下,它被编写为使用转义键,使用 conio 函数的改编,但您可以根据您的目的进行调整)

Following is an example from here that illustrates how you might run a loop that consumes input until non-garbage appears over the keyboard... (in this case, it is written to work with escape key, using an adaptation of conio functions, but you can adapt it for your purposes)

   do
   {
      ch = _getch();
      ch = toupper( ch );

      if (ch != 27) {
         _cputs( "CHARACTER: " );
         _putch( ch );
         _putch( '\r' );    // Carriage return
         _putch( '\n' );    // Line feed
      }

   } while( ch != 27 ); 

原帖
这没有回答 有没有办法检查标准输入缓冲区中是否有垃圾,但它解决了如何确保它是清除...
添加一行让用户清楚如何开始,并且由于您的原始代码,这将导致清除任何垃圾的缓冲区,或在用户被指示输入其他值之前的意外输入...:...

Original post
This does not answer Is there a way of checking if there is garbage in the stdin-buffer, but it addresses how to make sure it is clear...
Add one additional line to make it clear to the user how to begin, and because of your original code, it will result in clearing the buffer of any garbage, or unintended input before user is directed to enter additional values...:...

    ...
    printf("hit <return> to begin");
    while ( (c = getchar()) != '\n' && c != EOF );
    printf("Please enter an arbitrary number of float or symbolic values:\n");    
    ...

这篇关于普通 C 标准输入缓冲区垃圾和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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