fflush的更换(标准输入) [英] Replacement of fflush(stdin)

查看:145
本文介绍了fflush的更换(标准输入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有低于code,

fflush(stdin);
print("Enter y/n");
scanf("%c",&a);

下面,那是因为它不冲洗掉它可能有一些垃圾characters.Is那里冲洗任何替代输入缓冲区退出之前给input.it看起来像问题(标准输入)。这$ C $ ç段工作在Solaris中,但它不是在Linux的工作。

Here,it is quitting before giving the input.it looks like the issue is because it is not flushing out the input buffer which might be having some junk characters.Is there any alternative for flush(stdin).This code snippet is working in Solaris but it is not working in Linux.

推荐答案

这是很好的Ç常见问题解答。另请参阅:解释。提出的解决方案:

This is well explained in the C FAQ. See also: explanation. The proposed solutions:


  • 退出使用scanf函数。使用与fgets 的sscanf

  • 使用此吃新行

  • Quit using scanf. Use fgets and the sscanf
  • Use this to eat the newline

while((c = getchar()) != '\n' && c != EOF)
/* discard the character */;


这冲洗标准输入上的一些实现的工作原理其实 是错

The fact that flushing stdin works on some implementations is wrong.

有些厂商确实实现fflush这样
  该fflush(标准输入)丢弃未读
  人物,虽然移植的程序
  不能依靠这一点。

Some vendors do implement fflush so that fflush(stdin) discards unread characters, although portable programs cannot depend on this.

这篇关于fflush的更换(标准输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆