C语言 我无法刷新标准输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2187474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:22:25  来源:igfitidea点击:

I am not able to flush stdin

cstdinfflush

提问by Subodh Asthana

How to flush the stdin??

如何刷新标准输入

Why is it not working in the following code snippet?

为什么它在以下代码片段中不起作用?

#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <fcntl.h>

int main()
{
        int i=0,j=0, sat;
        char arg[256];
        char * argq;
        argq = malloc(sizeof(char)*10);

        printf("Input the line\n");
        i=read(0, arg, sizeof(char)*9);
        arg[i-1]='
int c;
while ((c = getchar()) != '\n' && c != EOF);
'; fflush(stdin); i=read(0, argq, sizeof(char)*5); argq[i-1]='
int c;
while((c = getchar()) != '\n' && c != EOF);
'; puts(arg); puts(argq); return 0; }

Now if i give the input as 11 characters, only 9 should be read but the remaining two characters in the stdin are not flushed and read again in the argq. Why?

现在,如果我将输入设为 11 个字符,则只应读取 9 个字符,但不会刷新 stdin 中的其余两个字符并在 argq 中再次读取。为什么?

Input: 123 456 789

输入:123 456 789

Output: 123 456 89

输出:123 456 89

Why am i getting this 89 as the output?

为什么我得到这个 89 作为输出?

回答by jschmier

I believe fflush is only used with output streams.

我相信 fflush 仅用于输出流。

You might try fpurgeor __fpurgeon Linux. Note that fpurge is nonstandard and not portable. It may not be available to you.

您可以在 Linux 上尝试fpurge__fpurge。请注意, fpurge 是非标准的且不可移植。您可能无法使用它。

From a Linux fpurge man page: Usually it is a mistake to want to discard input buffers.

来自Linux fpurge 手册页:通常想要丢弃输入缓冲区是错误的。

The most portable solution for flushing stdin would probably be something along the lines of the following:

刷新标准输入最便携的解决方案可能是以下几行:

void readLine(char* input , int nMaxLenIncludingTerminatingNull )
{
    fgets(input, nMaxLenIncludingTerminatingNull , stdin);

    int nLen = strlen(input);

    if ( input[nLen-1] == '\n' )
        input[nLen-1] = '##代码##';
}

std::string readLineToStdString(int nMaxLenIncludingTerminatingNull)
{
    if ( nMaxLenIncludingTerminatingNull <= 0 )
        return "";

    char* input = new char[nMaxLenIncludingTerminatingNull];
    readLine(input , nMaxLenIncludingTerminatingNull );

    string sResult = input;

    delete[] input;
    input = NULL;

    return sResult;
}

回答by jamesdlin

##代码##

Is how I'd clear the input buffer.

是我清除输入缓冲区的方式。

回答by dirkgently

How to flush the stdin??

如何刷新标准输入?

Flushing input streams is invoking Undefined Behavior. Don't try it.

刷新输入流正在调用未定义的行为。不要尝试。

You can only flush output streams.

您只能刷新输出流。

回答by Nikolai Fetissov

You are overriding the last element of the input in argwith '\0'. That line should be arg[i]='\0';instead (after error and boundary checking you are missing.)

要覆盖在输入的最后一个元素arg'\0'。那条线应该是arg[i]='\0';(在错误和边界检查之后你丢失了。)

Other's already commented of the flushing part.

其他人已经评论了冲洗部分。

回答by Sunny127

You can't clean stdin in Linux without bumping into scenarios that the command will start waiting for input in some cases. The way to solve it is to replace all std::cin with readLineToStdString():

您无法在 Linux 中清理 stdin,而不会遇到命令在某些情况下开始等待输入的情况。解决方法是将所有 std::cin 替换为 readLineToStdString():

##代码##

This will also allow you to enter spaces in std::cin string.

这也将允许您在 std::cin 字符串中输入空格。

回答by Ng? Thái S?n

In Windows you can use rewind(stdin)fuction.

在 Windows 中,您可以使用rewind(stdin)功能。