while (TRUE) { char cmdbuf[4]; fgets(cmdbuf,4,stdin); //这里输入了许多数据,大于4,然后回车。 //我想判断标准输入缓冲区是否有数据?如果有我就循环调用getchar()清空缓冲区数据。 // int c; //while((c = getchar()) != '\n' && c != EOF);
//但是如果没有数据的话,getchar这里会等待输入。 //问题就是怎么判断标准输入缓冲区是否有数据? printf("%s\n",cmdbuf); }
Portably, you can get the next character in the input stream with getchar() and then push it back with ungetc(), which results in a state as if the character wasn't removed from the stream.
or you could use setvbuf on stdin with your own buffer, so you can peek whenever you want
|