Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C fflush()在Linux中不起作用_C_Linux_Gcc - Fatal编程技术网

C fflush()在Linux中不起作用

C fflush()在Linux中不起作用,c,linux,gcc,C,Linux,Gcc,我在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?");

我在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)
  }
然后节目结束。这就是全部。在Linux中我可以做什么?有替代函数吗?

fflush()
对输入流没有多大作用,但由于
scanf()
从不返回,这并不重要
scanf()
阻塞,因为在按Enter键之前,终端窗口不会向C程序发送任何内容

您有两个选择:

  • 键入
    10
    Enter
  • 将终端置于原始模式

  • 第二个选项有许多缺点,例如您将失去编辑功能,因此我建议逐行读取输入。

    不要使用fflush,而是使用此功能:

    #include <stdio.h>
    void clean_stdin(void)
    {
        int c;
        do {
            c = getchar();
        } while (c != '\n' && c != EOF);
    }
    
    #包括
    无效清洁标准(无效)
    {
    INTC;
    做{
    c=getchar();
    }而(c!='\n'&&c!=EOF);
    }
    

    fflush(stdin)
    取决于实现,但此函数始终有效。在C语言中,使用
    fflush(stdin)

    被认为是不好的做法,
    fflush
    的行为没有为输入流定义():

    7.21.5.2
    fflush
    功能

    简介

    1<代码> #包括 int-fflush(文件*流); 说明

    2如果流指向输出流或更新流,其中最近的 未输入操作,
    fflush
    函数会导致该流的任何未写入数据 发送到主机环境并写入文件;否则,该行为是错误的 未定义。

    3如果
    stream
    是空指针,则
    fflush
    函数对所有 上面定义了行为的流。

    返回

    4.
    fflush
    函数设置流的错误指示器,并在写入错误时返回EOF 发生错误,否则返回零。
    您必须包含并使用u fpurge(无论您想要什么)


    来自阿根廷的致敬

    一款始终在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);
    }
    
    #包括
    #包括
    void clean_stdin()
    {
    int stdin_copy=dup(stdin_文件号);
    /*从标准数据库中清除垃圾*/
    tcdrain(标准副本);
    tcflush(标准副本,TCIFLUSH);
    关闭(标准副本);
    }
    

    您不仅可以将tcdrain和tcflush用于in/out/err fd。

    在LINUX上工作时,我遇到了相同的问题,这个问题的另一个解决方案是定义一个虚拟字符,比如说
    char dummy
    在实际输入发生之前,放置一个
    scanf()
    来扫描它。这对我有用。我希望它也能为您工作。

    使用
    bzero()系统调用在Linux中,我们可以刷新以前的存储值。
    请阅读
    bzero()的手册页man bzero
    中键入code>。 试试这个例子

    #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;
    } 
    
    #包括
    #包括
    int main()
    {
    char buf[]={'y'};
    int-num;
    而(buf[0]=='y')
    {
    printf(“输入编号”);
    scanf(“%d”和&num);
    printf(“%d的平方是%d\n”,num,num*num);
    b0(buf,1);
    printf(“想要输入是/否”);
    scanf(“%s”和&buf[0]);
    }
    返回0;
    } 
    
    在scanf之后使用getchar()

    \include
    
    #include<stdio.h>
    int main()
    {
    char ans='y';
    int a;
    while(ans=='y'||ans=='Y')
        {
    
         printf("Type a number:-");
         scanf("%d",&a);
         printf("square of number = %d\nwant to enter 
         number again(y/n)?\nANS=",a*a); 
         scanf("%s",&ans);//use %s in place of %c
         }
    return 0;
    }
    
    int main() { char ans='y'; INTA; 而(ans=='y'| ans=='y') { printf(“键入编号:-”); scanf(“%d”和“&a”); printf(“数字的平方=%d\n要输入吗 再次编号(是/否)?\nANS=“,a*a); scanf(“%s”,&ans);//使用%s代替%c } 返回0; }
    请记住,刷新stdin实际上是未定义的行为。不要这样做。不要
    fflush(stdin)
    。相反,将
    scanf()
    更改为
    fgets()
    @Stefan我只是尝试使用fflush()刷新输入流。在windows中它工作,但在linux中它不工作…@sundar是的,但是为什么要刷新输入流?除非你说你需要程序做什么,否则不可能推荐一个替代方案。
    fflush()
    是输入流上一个未定义的操作。作为扩展,某些编译器可能会支持这种未定义的行为。我不建议依赖于此。
    fflush(stdin)
    是一种不好的做法,而不是
    fflush
    )@md5你说的“不是”是什么意思。你的意思是说这是一种好的练习吗?或者不存在没有参数的fflush?@barlop:
    fflush
    可能有用,例如,对于输出流:
    fflush(stdout)
    强制写入输出(请参阅)。这里它不是一个未定义的行为。你能解释它是如何工作的,而不是仅仅说“不要使用那个,使用这个,它工作”?你应该解释为什么你需要包括它,而不是仅仅说你需要。这段代码会导致
    scanf(“%s”
    行上的缓冲区溢出。另外,
    buf[0]=0;
    bzero(buf,1)
    fflush
    在POSIX.1-2001中未定义输入流,但在POSIX.1-2008中定义了它。请在回答时添加一些说明
    #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;
    } 
    
    #include<stdio.h>
    int main()
    {
    char ans='y';
    int a;
    while(ans=='y'||ans=='Y')
        {
    
         printf("Type a number:-");
         scanf("%d",&a);
         printf("square of number = %d\nwant to enter 
         number again(y/n)?\nANS=",a*a); 
         scanf("%s",&ans);//use %s in place of %c
         }
    return 0;
    }