相关疑难解决方法(0)

我无法冲洗stdin

如何刷新stdin

为什么它不能在以下代码片段中工作?

#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]='\0';
        fflush(stdin);

        i=read(0, argq, sizeof(char)*5);
        argq[i-1]='\0';

        puts(arg);
        puts(argq);

        return 0;
}
Run Code Online (Sandbox Code Playgroud)

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

输入:123 456 789

产出:123 456 89

为什么我将这89作为输出?

c stdin fflush

17
推荐指数
3
解决办法
5万
查看次数

标签 统计

c ×1

fflush ×1

stdin ×1