如何清空标准输入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;
}
如果我输入11个字符,只应该读取其中的9个,但是stdin中剩余的两个字符没有被清除,会在argq中再次被读取。为什么?
输入:123 456 789
输出:
123 456
89
为什么我会得到输出值为89
?