fflush()不能在Linux下工作 [英] fflush() is not working in Linux

查看:615
本文介绍了fflush()不能在Linux下工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用了fflush()在Linux中GCC,但没有奏效。是否有该功能的其它方法吗?这里是我的code:

I used the fflush() in Linux GCC but it did not work. Are there any alternatives for that function? Here is my code:

#include<stdio.h>
void main()
{
  char ch='y';
  while(ch=='y')
  {
    int a;
    printf("Enter some value:");
    scanf("%d",&a);
    fflush(stdin);
    printf("Do you want to continue?");
    scanf("%c",&ch)
  }

这是我得到的输出是:

Enter some value: 10

然后该程序结束。就这样。我可以在Linux中做什么?是否有其他的功能?

Then the program ends. That's all. What can I do in Linux? Is there an alternative function?

推荐答案

不要使用fflush,使用此功能来代替:

Don't use fflush, use this function instead:

#include <stdio.h>
void clean_stdin(void)
{
    int c;
    do {
        c = getchar();
    } while (c != '\n' && c != EOF);
}

fflush(标准输入)取决于实施,但这一功能总是有效。在C中,它被认为是不好的做法,使用 fflush(标准输入)

fflush(stdin) depends of the implementation, but this function always works. In C, it is considered bad practice to use fflush(stdin).

这篇关于fflush()不能在Linux下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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