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
");
i=read(0, arg, sizeof(char)*9);
arg[i-1]='';
fflush(stdin);
i=read(0, argq, sizeof(char)*5);
argq[i-1]='';
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?
Input:
123 456 789
Output:
123 456
89
Why am i getting this 89 as the output?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…