rewind(stdin)

rewind(stdin)清除标准输入的按键缓冲区。

fflush()清除I/O流级别的缓冲区。

先看看Microsoft给出的例子:

#include <stdio.h>
void main(void)
{
	int ch ;
	
	puts( "Input two or more chars. One is read now." );
	ch = getchar();
	putchar( ch );
	putchar( '\n' );
	
	puts( "The next char is taken from stdin." );
	ch = getchar( );
	putchar( ch );
	putchar( '\n' );
	
	rewind( stdin );
	puts( "Input two or more chars. One is read now." );
	ch = getchar();
	putchar( ch );
	putchar( '\n' );
	
	rewind( stdin );
	puts( "You must enter another char because of the rewind(stdin)" );
	ch = getchar( );
	putchar( ch );
	putchar( '\n' );
}

再看:

int main()
{
	char name[6] = {'\0'};
	char password[6] = {'\0'};
	int count = 0;
	
	while(1)
	{
		count++;
		memset(name, 0, sizeof(name));
		memset(password, 0, sizeof(password));

		scanf("%5[^\n]", name);
		if(strcmp(name, "exit") == 0)
			break;
//		rewind(stdin);
		fflush(stdin);
//		getchar();

 		scanf("%5[^\n]", password);
// 		rewind(stdin);
		fflush(stdin);
//		getchar();
		printf("name=[%s],password=[%s]\n",name,password);

		if(count>5)break;
	}
	return 0;
}

若将fflush(stdin)改成rewind(stdin)测试效果相同。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值