C90 printf,在cygwin中\n或\r\n不工作;但是fflush(stdout);很好。为什么?

C90 printf,在cygwin中\n或\r\n不工作;但是fflush(stdout);很好。为什么?,c,gcc,cygwin,printf,C,Gcc,Cygwin,Printf,Cygwin64位 要编译的命令: gcc hello.c -o hello -ansi -pedantic-errors 运行命令 ./hello 你好,c #include<stdio.h> int main() { /*setbuf(stdout, 0); I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not w

Cygwin64位

要编译的命令:

gcc hello.c -o hello -ansi -pedantic-errors
运行命令

./hello
你好,c

#include<stdio.h>
int main() {
    /*setbuf(stdout, 0);        I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
    printf("hello world!\n");
    printf("hello world again!\r\n");
    /*fflush(stdout);           without fflush, the above strings are not showing*/

    while(1)
    {
    }
}
#包括
int main(){
/*setbuf(stdout,0);我知道如果添加它,它会起作用,这是一个解决方案,但我想知道为什么换行符本身不起作用*/
printf(“你好,世界!\n”);
printf(“再次向世界问好!\r\n”);
/*fflush(stdout);如果没有fflush,则不会显示上述字符串*/
而(1)
{
}
}
问题:

  • 我不想在每次printf之后都使用fflush让终端及时显示字符串,那么怎么做呢

    回答:setbuf(标准差,0)

  • 为什么“\n”或“\r\n”在我的案例中不起作用,因为很多帖子指出换行可以解决问题


  • cygwin的终端行为是否与普通Linux的终端行为不同?因为我没有安装linux,有人给我测试吗

  • 或者让我问一个更一般的问题:在哪种终端上,“一条新线路将强制刷新”这句话是真的


  • 谢谢

    我对cgywin一无所知。但是这里我在Linux中做了一个测试

    我尝试下面的代码,并通过以下方式编译:gcc-std=c90 filename.c

    没有fflush,它会在循环之前打印所有单词,所以我认为换行符会刷新缓冲区!它只是工作

    我使用SUSE gcc

    #include<stdio.h>
    int main() {
        /*setbuf(stdout, 0);        I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
        printf("hello world!\n"); /*without fflush, not shown*/
        printf("hello world again!\r\n"); /*without fflush, not shown*/
      /* fflush(stdout);*/
    
        while(1)
        {
        }
    }
    
    #包括
    int main(){
    /*setbuf(stdout,0);我知道如果添加它,它会起作用,这是一个解决方案,但我想知道为什么换行符本身不起作用*/
    printf(“hello world!\n”);/*不带fflush,未显示*/
    printf(“再次向世界问好!\r\n”);/*不带fflush,未显示*/
    /*fflush(stdout)*/
    而(1)
    {
    }
    }
    
    我对cgywin一无所知。但是这里我在Linux中做了一个测试

    我尝试下面的代码,并通过以下方式编译:gcc-std=c90 filename.c

    没有fflush,它会在循环之前打印所有单词,所以我认为换行符会刷新缓冲区!它只是工作

    我使用SUSE gcc

    #include<stdio.h>
    int main() {
        /*setbuf(stdout, 0);        I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
        printf("hello world!\n"); /*without fflush, not shown*/
        printf("hello world again!\r\n"); /*without fflush, not shown*/
      /* fflush(stdout);*/
    
        while(1)
        {
        }
    }
    
    #包括
    int main(){
    /*setbuf(stdout,0);我知道如果添加它,它会起作用,这是一个解决方案,但我想知道为什么换行符本身不起作用*/
    printf(“hello world!\n”);/*不带fflush,未显示*/
    printf(“再次向世界问好!\r\n”);/*不带fflush,未显示*/
    /*fflush(stdout)*/
    而(1)
    {
    }
    }
    
    在Cygwin中,
    stdout
    似乎没有被标识为终端(而是管道),因此默认情况下它不是行缓冲的


    基于,与Cygwin DLL链接可能会有所帮助。

    在Cygwin中,
    stdout
    似乎没有标识为终端(而是管道),因此默认情况下它不是行缓冲的


    基于,也许链接Cygwin DLL会有所帮助。

    该程序在32位Cygwin下为我工作。具体来说,当我编译和执行此程序时:

    #include<stdio.h>
    int main() {
        /*setbuf(stdout, 0);        I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
        printf("hello world!\n");
        printf("hello world again!\r\n");
        /*fflush(stdout);           without fflush, the above strings are not showing*/
    
        while(1)
        {
        }
    }
    
    然后挂起,直到我用Ctrl-C杀死它

    我在Windows控制台、mintty和xterm下从bash调用程序时也会得到同样的行为(我怀疑终端是否会有任何不同)

    我在64位Windows7下使用32位Cygwin。你说你正在使用64位Cygwin,这是;我还没试过

    我怀疑64位与32位Cygwin存在问题。我建议你把它发到网上

    以下是您的程序的清理版本,该版本应显示相同的问题(请验证注释是否正确):

    #包括
    内部主(空){
    /*在此处添加setbuf(stdout,0)将显示输出*/
    printf(“你好,世界!\n”);
    /*在此处添加fflush(stdout)将显示输出*/
    而(1){
    /*没什么*/
    }
    }
    
    该程序在32位Cygwin下为我工作。具体来说,当我编译和执行此程序时:

    #include<stdio.h>
    int main() {
        /*setbuf(stdout, 0);        I KNOW THIS WILL WORK IF ADDED, it is a solution, but I want to know why line break itself is not working*/
        printf("hello world!\n");
        printf("hello world again!\r\n");
        /*fflush(stdout);           without fflush, the above strings are not showing*/
    
        while(1)
        {
        }
    }
    
    然后挂起,直到我用Ctrl-C杀死它

    我在Windows控制台、mintty和xterm下从bash调用程序时也会得到同样的行为(我怀疑终端是否会有任何不同)

    我在64位Windows7下使用32位Cygwin。你说你正在使用64位Cygwin,这是;我还没试过

    我怀疑64位与32位Cygwin存在问题。我建议你把它发到网上

    以下是您的程序的清理版本,该版本应显示相同的问题(请验证注释是否正确):

    #包括
    内部主(空){
    /*在此处添加setbuf(stdout,0)将显示输出*/
    printf(“你好,世界!\n”);
    /*在此处添加fflush(stdout)将显示输出*/
    而(1){
    /*没什么*/
    }
    }
    
    我刚刚遇到了这个问题。我有一些代码已经编写了一段时间,不得不在我的电脑上重新安装Cygwin。这次我安装了64位版本,以前是x86

    构建我的代码,在bashshell中运行,没有输出

    我没有意识到的是,我的bin文件夹中还有以前32位版本的cygwin的Cygwin1.dll。我重新命名了它,运行了我的程序,输出正常


    您可能有不同的问题,但如果您的bin或其他文件夹中使用了不同的dll版本,则可能会导致此问题。

    我刚刚遇到此问题。我有一些代码已经编写了一段时间,不得不在我的电脑上重新安装Cygwin。这次我安装了64位版本,以前是x86

    构建我的代码,在bashshell中运行,没有输出

    我没有意识到的是,我的bin文件夹中还有以前32位版本的cygwin的Cygwin1.dll。我重新命名了它,运行了我的程序,输出正常


    您可能会遇到不同的问题,但如果您的bin或其他文件夹中使用了不同的dll版本,则可能会导致此问题。

    这可能是重复的:我对cygwin的体验是,它接近于Linux,但无论您如何努力,都无法实现。可能是内核没有检测到您正在进行终端会话。也许可以尝试另一种方法