fflush() 在 Linux 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17318886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:18:35  来源:igfitidea点击:

fflush() is not working in Linux

clinuxgcc

提问by sundar

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

我在 Linux GCC 中使用了 fflush() 但它不起作用。该功能是否有任何替代方案?这是我的代码:

#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)
  }

The output that I got is:

我得到的输出是:

Enter some value: 10

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

然后程序结束。就这样。我可以在 Linux 中做什么?有替代功能吗?

采纳答案by Mathuin

Don't use fflush, use this function instead:

不要使用 fflush,而是使用这个函数:

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

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

fflush(stdin)取决于实现,但此功能始终有效。在 C 中,使用fflush(stdin).

回答by Aaron Digulla

fflush()doesn't do much for input streams but since scanf()never returns this doesn't matter. scanf()blocks because the terminal window doesn't send anything to the C program until you press Enter

fflush()对输入流没有太大作用,但由于scanf()从不返回这无关紧要。scanf()阻止是因为终端窗口在您按下之前不会向 C 程序发送任何内容Enter

You have two options:

您有两个选择:

  1. Type 10Enter
  2. Put the terminal into raw mode.
  1. 类型 10Enter
  2. 将终端置于原始模式。

The second option has many drawbacls like you will lose editing capabilities, so I suggest to read the input line by line.

第二个选项有很多缺点,比如你会失去编辑能力,所以我建议逐行阅读输入。

回答by John Bode

The behavior of fflushis not defined for input streams (online 2011 standard):

的行为fflush未为输入流定义(在线 2011 标准):

7.21.5.2 The fflushfunction

Synopsis

1

    #include <stdio.h>
    int fflush(FILE *stream);
Description

2 If stream points to an output stream or an update stream in which the most recent operation was not input, the fflushfunction causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

3 If streamis a null pointer, the fflushfunction performs this flushing action on all streams for which the behavior is defined above.

Returns

4 The fflushfunction sets the error indicator for the stream and returns EOF if a write error occurs, otherwise it returns zero.

回答by user3579333

You must include and use __fpurge(whatever you want) instead.

您必须包含并使用 __fpurge(whatever you want) 来代替。

Salute from argentina

来自阿根廷的敬礼

回答by ataraxic

One that always works on Linux:

一种始终适用于 Linux 的方法:

#include <termios.h>
#include <unistd.h>

void clean_stdin()
{
        int stdin_copy = dup(STDIN_FILENO);
        /* remove garbage from stdin */
        tcdrain(stdin_copy);
        tcflush(stdin_copy, TCIFLUSH);
        close(stdin_copy);
}

You can use tcdrainand tcflushnot only for in/out/err fd.

您不仅可以将tcdraintcflush用于输入/输出/错误 fd。

回答by Akshdeep Shubham

I faced the same problem while working on LINUX and an alternative solution of this problem can be that you define a dummy character lets say char dummy;and put a scanf()to scan it just before your actual input takes place. This worked for me. I hope it would work for you too.

我在 LINUX 上工作时遇到了同样的问题,这个问题的替代解决方案可以是你定义一个虚拟字符,让我们说,char dummy;scanf()在实际输入发生之前放置一个扫描它。这对我有用。我希望它也对你有用。

回答by Raj Ravi

By using bzero();system call in Linux we can flush the previous stored value.
Please read the manual page of bzero();by typing in terminal man bzero. try this example

通过bzero();在 Linux 中使用系统调用,我们可以刷新之前存储的值。
bzero();通过输入终端阅读手册页man bzero。试试这个例子

#include<stdio.h>
#include<string.h>

int main()
{
  char buf[]={'y'};
  int num;
  while(buf[0]=='y')
  {
    printf("enter number");
    scanf("%d",&num);
    printf("square of %d is %d\n",num,num*num);
    bzero(buf, 1);
    printf("want to enter y/n");
    scanf("%s",&buf[0]);
  }
  return 0;
} 

回答by Ahlecksz9

Use getchar() instead, after scanf

在 scanf 之后改用 getchar()