其他分享
首页 > 其他分享> > c – 虽然禁用了缓冲,但Printf不会立即打印

c – 虽然禁用了缓冲,但Printf不会立即打印

作者:互联网

我在C中的printf函数有问题.虽然缓冲被禁用,但它不打印输出:

setbuf(stdout, NULL);  

setvbuf(stdout, NULL, _IONBF, 0);

我也在使用fflush(stdout);但它仍然不起作用.
这是确切的代码:

int setup(){
    //...
    printf("Setup successful\n");
    fflush(stdout);
    return 0;
}
int main(int argc, char *argv[]){
    setbuf(stdout, NULL);
    setvbuf(stdout, NULL, _IONBF, 0);
    setup();
    //...
)

如果信息有帮助;我在Linux(raspberry Pi)上.
提前致谢!

解决方法:

我尝试过尽可能地重现您的设置.因此,我在一个VirtualBox图像中安装了Raspbian(Raspberry Pi操作系统),并使用Geany创建,编译和执行C文件.以下是完整的代码:

#include <stdio.h>

int main() {
    printf("Setup successful\n");
}

>将此文件另存为test.c:

test.c saved
>接下来,单击“Build”(砖块图标):

build test.c
>最后,运行它(单击纸平面图标):

enter image description here

如您所见,此代码正确编译,执行并打印消息.不需要明确的冲洗(printf to stdout automatically flushes when encountering a newline character).这种行为是标准化的,并且由Raspbian安装的工具正确实现,因此它是可靠的.

标签:c-3,raspberry-pi3,linux,printf,buffer
来源: https://codeday.me/bug/20190731/1587296.html