C:Stdin-删除多余的数据 [英] C: Stdin - Removing Excess Data

查看:100
本文介绍了C:Stdin-删除多余的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本

我想摆脱stdin中多余的用户输入,但我理解不建议使用fflush(stdin).我找到了这个替代品:

I want to get rid of excess user input from stdin but I understand fflush(stdin) is not recommended. I found this replacement:

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

但是,如果没有多余的数据要删除,这将删除用户的下一个输入.如何检查是否有要从stdin中删除的数据,以便可以选择是否使用这种清除缓冲区的方法?

However, if there is no excess data to remove, this will remove the user's next input. How can I check if there is data to remove from stdin so I can choose whether or not to use this method of clearing the buffer?

带说明的长版

我正在用Obj-C/C制作命令行工具.

I'm making a command-line tool in Obj-C/C.

我知道声明char str[SIZE]语法会将SIZE字节的内存分配给str.我想为每个变量分配尽可能少的内存,以避免浪费内存.

I understand that declaring char str[SIZE] syntax allocates SIZE bytes of memory to str. I'd like to allocate as little memory as possible to each variable in order to avoid wasting memory.

但是,如果您使用fgets(str,sizeof(str),stdin),并且输入的更多比输入的SIZE字节多,则fgets将占用输入的SIZE个字节并将其存储在str中,将所有其他数据保留在缓冲区中.如果再次调用fgets,剩余的数据将存储在作为fgets的第一个变量传递的变量中,并且实际上完全跳过了提示.我不希望这种情况发生.常见的解决方法是fflush(stdin).

However, if you use fgets(str,sizeof(str),stdin) and you enter more than SIZE bytes of input, fgets will take SIZE bytes of your input and store that in str, leaving all the other data in the buffer. If you call fgets again, the remaining data will be stored in the variable you pass as fgets's first variable and essentially skip the prompt completely. I don't want this to happen. A common solution is fflush(stdin).

各种Internet资源(包括SO)指出,使用fflush(stdin)并不是一个好主意,因为当您将输入流作为参数传递时,它是未定义的".

Various internet resources (including SO) state that using fflush(stdin) is not a good idea because it is "undefined" when you pass an input stream as argument.

我在 CProgramming.com上找到了该替换项

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

但是,同一消息源提到,如果stdin中没有多余的数据要消除,它将使用用户的下一个输入来执行此操作,因此第二次调用fgets时,它可能不会接收到所有数据.

However, the same source mentions that if there is no excess data in stdin to get rid of, it will do this with the user's next input, so the second time fgets is called it might not receive all the data.

如何检查stdin中是否有数据,以便我决定是否清除它?

How can I check if there is data in stdin so I can decide whether or not to clear it?

推荐答案

如果使用 fgets 以阅读行,并且您担心自己可能没有阅读整行,请记住,如果

If you use fgets to read lines, and you're worried that you might not have read the whole line, remember that if fgets read the whole line the newline character should be the last character in the input buffer.

示例:

char smallBuffer[10];
while (fgets(smallBuffer, sizeof(smallBuffer), stdin) != NULL)
{
    if (strlen(smallBuffer) > 0)
    {
        // Check if we got the whole line
        if (smallBuffer[strlen(smallBuffer) - 1] == '\n')
        {
            // Yes, we got the whole line
        }
        else
        {
            // Whole line not read, there is still "excess" input
        }
    }
}

这篇关于C:Stdin-删除多余的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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