怎样清空标准输入缓冲区?

zyp80 2004-07-16 11:29:26
RT
我的程序在客户端阻塞在等待读取数据的时候,如果输入了东西

等读取到数据后,要从标准屏幕输入时,就读取了阻塞时不小心输入的东西

请问是不是要清空标准输入缓冲区,用什么语句,多谢

还有有没有办法清空socket读取数据的缓冲区,socket读取显示的字符串经常出现乱码

多谢指教
...全文
1208 40 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyp80 2004-07-19
  • 打赏
  • 举报
回复
sorry,linux下没有fflushall()好像

而且我不是这个意思,我是要把输入的都不算,包括回车
tuxw 2004-07-19
  • 打赏
  • 举报
回复
清其它的缓冲可以用 fflushall()
tuxw 2004-07-19
  • 打赏
  • 举报
回复
编译时可能要将双斜杠换成 /**/ 对
tuxw 2004-07-19
  • 打赏
  • 举报
回复
看这样行不和行

#include <stdio.h>
#include <conio.h>

void main()
{
char c1, c2;

fflush( stdin ); // 清缓冲
scanf( "%c", &c1 );

fflush( stdin ); // 2
scanf( "%c", &c2 );

printf( "c1 = %c\tc2 = %c\n", c1, c2 );

getch();
}

输入
abcde // 回车
ghik // 回车

输出
c1 = a c2 = g

如果去掉注释 2 处的一行,应该输出
c1 = a c2 = b
zyp80 2004-07-19
  • 打赏
  • 举报
回复
to:geesun(发芽的石头)
你的方法很可行,我原来好像也这么做过,我想想是怎么弄的,好像是ioctl什么的,多谢。
zyp80 2004-07-19
  • 打赏
  • 举报
回复
to: whyglinux()
fget只能读一行,书上也是这么说的
我要连续输入就不行了
zyp80 2004-07-19
  • 打赏
  • 举报
回复
to: universe01(未知软体)
按你的程序,我的运行结果可不是这样的,不知道为什么
运行如下
12 23
please insert a int
you insert is 23
peter9606 2004-07-19
  • 打赏
  • 举报
回复
但是linux下有fflush()
zyp80 2004-07-19
  • 打赏
  • 举报
回复
多谢各位,周末没有上网,可问题还是没有解决
to:bbants() ,linux下没有flushall,man不到,也不能调用
zyp80 2004-07-19
  • 打赏
  • 举报
回复
解决了,不容易
tcflush(0,TCIFLUSH);
0就是标准输入,就是STDIN_FILENO,就可以清空缓冲区了
tcflow这个函数可以屏蔽输入

多谢大家帮忙,结帖
universe01 2004-07-17
  • 打赏
  • 举报
回复
#include<stdio.h>
int main()
{
int a,b,c;
scanf("%d", &c);
printf("please insert a int\n");
//fflush(stdin);
scanf("%d",&a);
printf("you insert is %d\n",a);
}
input: 12 23
直接输出: 23

#include<stdio.h>
int main()
{
int a,b,c;
scanf("%d", &c);
printf("please insert a int\n");
fflush(stdin);
scanf("%d",&a);
printf("you insert is %d\n",a);
}
运行时输入:12 23回车
出现"please insert a int"提示
然后输入40回车
out:40


行僧 2004-07-17
  • 打赏
  • 举报
回复
我试过,用flushall(),放在scanf()前用!很好用!
whyglinux 2004-07-16
  • 打赏
  • 举报
回复
最近刚好在CU回答了这个问题,见这里http://bbs.chinaunix.net/forum/viewtopic.php?t=352144
geesun 2004-07-16
  • 打赏
  • 举报
回复
it seems not probable to flush either input buffer or output buffer. Keyboard mayb implemented using i/o port 0x60 and 0x64. I do not remember there is any commands which can b issued to flush the buffer, so I do not think the related API exists. But mayb u can first disable the keyboard then enable it to flush both buffers.
softcar 2004-07-16
  • 打赏
  • 举报
回复
Example
/* FFLUSH.C */

#include <stdio.h>
#include <conio.h>

void main( void )
{
int integer;
char string[81];

/* Read each word as a string. */
printf( "Enter a sentence of four words with scanf: " );
for( integer = 0; integer < 4; integer++ )
{
scanf( "%s", string );
printf( "%s\n", string );
}

/* You must flush the input buffer before using gets. */
fflush( stdin );
printf( "Enter the same sentence with gets: " );
gets( string );
printf( "%s\n", string );
}

Output
Enter a sentence of four words with scanf: This is a test
This
is
a
test
Enter the same sentence with gets: This is a test
This is a test

msdn的例子
zyp80 2004-07-16
  • 打赏
  • 举报
回复
请高人指点,正确的实现方法
peter9606 2004-07-16
  • 打赏
  • 举报
回复
一句话
在正确数据来之前 fflush()一下
zyp80 2004-07-16
  • 打赏
  • 举报
回复
多接收几次?那接收几次?接收多了怎么办,有用的不就没了,而且还阻塞了
zyp80 2004-07-16
  • 打赏
  • 举报
回复
我没用过GDB,看半天也没看明白

我先在想知道这个问题怎么解决
softcar 2004-07-16
  • 打赏
  • 举报
回复
不过楼上的方法时应该是可以解决问题的,只是示例不太恰当.
加载更多回复(20)

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧