如何在获得新输入之前清除标准输入? [英] How to clear stdin before getting new input?

查看:31
本文介绍了如何在获得新输入之前清除标准输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了大约 5 到 10 条不同的关于如何清除 stdin 的建议,但没有一条适合我的需求.问题是 fflush(stdin) 在我的电脑上运行得很好,但不幸的是它似乎并不是在任何地方都可以运行,所以我需要一些具有相同功能的东西.我尝试过的所有其他方式在 stdin 不为空时都会清除它,但在 stdin 为空时需要用户输入,这意味着它需要在我不想得到任何输入的时候输入(+ 无论如何它都会丢弃它).

I have read about 5-10 different advices how to clear stdin, but none of them suits my needs. The thing is that fflush(stdin) worked perfectly at my computer, but unfortunately it doesn't seem to work everywhere, so I need something with the same functionality. Every other way I tried clears stdin when it is not empty but requires user input when stdin IS empty, which means it requires input in a moment I dont want to get any (+ it discards it anyway).

问题是:在我需要用户输入之前,我能否以某种方式确保 stdin 为空?(如果没有,那么然后才以某种方式清除它?)类似的东西:

The question is: Can I somehow make sure, that stdin IS empty before I require user input? (and if not, THEN and only then clear it somehow?) something like:

if (stdin is NOT empty) 
    while (getchar() != '\n')
        continue;

问题是我从 stdin 一个一个地加载字符,在某些时候,前一次迭代的一部分输入可能会或可能不会被丢弃.无论哪种方式,在我要求用户处理另一个输入之前,我都需要清除 stdin.清除缓冲区本身并不是什么大问题,问题是当程序到达清除 stdin 点时输入为空时会发生什么,因为在那一刻程序需要另一个输入将被清除功能吃掉.这就是我想要摆脱的.(当我可以使用 fflush(stdin); 我只知道,对于我的程序的下一行,stdin 无论如何都会是空的,没有问题.......)

the thing is that I load characters from stdin one by one and at some point, a part of the input from previous iteration might or might not get discarded. either way, I need to have clear stdin before I ask the user for another input to be processed. Clearing the buffer itself is not such a big deal, the problem is what happens when the input is empty when the program gets to the point of clearing stdin, because in that moment the program needs another input which is going to be eaten by the clearing function. Thats what I want to get rid of. (when I could use fflush(stdin); I just knew, that for the next line of my program the stdin WILL be empty no matter what, no questions asked...)

推荐答案

如何在获得新输入之前清除标准输入?
.. 所以我需要一些具有相同功能的东西.

How to clear stdin before getting new input?
.. so I need something with the same functionality.

对于便携式 C,这是不可能的.

With portable C this is not possible.

相反,建议使用不同的(更常见的 C)范式:
确保先前的输入函数消耗所有先前的输入.

Instead suggest a different (and more usual C) paradigm:
Insure previous input functions consumes all the previous input.

fgets()(或 *nix getline()) 是典型的方法,可以解决大多数情况.

fgets() (or *nix getline()) is the typical approach and solves most situations.

或者自己动手.下面读取整行,但不保存额外的输入.

Or roll your own. The following reads an entire line, but does not save extra input.

int mygetline(char *buf, size_t size) {
  assert(size > 0 && size <= INT_MAX);
  size_t i = 0;
  int ch;
  while ((ch = fgetc(stdin)) != EOF) {  // Read until EOF ...
    if (i + 1 < size) {
      buf[i++] = ch;
    }
    if (ch == '\n') {  // ... or end of line
      break;  
    }
  } 
  buf[i] = '\0';
  if (i == 0) { 
    return EOF;
  }
  return i;
}

这篇关于如何在获得新输入之前清除标准输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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