C相当于autoflush(每次写入后刷新stdout)?

ral*_*ldi 30 c stdio

在Perl中,我可以键入:

$|++;
Run Code Online (Sandbox Code Playgroud)

打印到STDOUT的任何内容都将自动fflush()编辑.

C中有等价物吗?换句话说,有什么方法可以告诉stdio在每次printf()之后自动刷新stdout,它会自动刷新stderr吗?

Har*_*rry 39

试试setvbuf(stdout, NULL, _IONBF, 0).它将更改stdout为unbuffered(_IONBF)模式.


yog*_*man 14

我没有这样做,但_IOLBF将是正确的答案.

$ man setvbuf
...
名称
setvbuf - 为流分配缓冲

大纲
#include <stdio.h>

$ man setvbuf
NAME
       setvbuf — assign buffering to a stream

SYNOPSIS
       #include <stdio.h>

       int setvbuf(FILE *restrict stream, char *restrict buf, int type,
           size_t size);

DESCRIPTION
       The functionality described on this reference page is aligned with
       the ISO C standard. Any conflict between the requirements described
       here and the ISO C standard is unintentional. This volume of
       POSIX.1?2008 defers to the ISO C standard.

       The setvbuf() function may be used after the stream pointed to by
       stream is associated with an open file but before any other operation
       (other than an unsuccessful call to setvbuf()) is performed on the
       stream. The argument type determines how stream shall be buffered, as
       follows:

        *  {_IOFBF} shall cause input/output to be fully buffered.

        *  {_IOLBF} shall cause input/output to be line buffered.

        *  {_IONBF} shall cause input/output to be unbuffered.
Run Code Online (Sandbox Code Playgroud)

描述
setvbuf()函数可以在stream指向的流与打开的文件关联之后但在对流执行任何其他操作(除了对setvbuf()的不成功调用之外)之前使用.参数类型确定如何缓冲流,如下所示:

$ man setvbuf
NAME
       setvbuf — assign buffering to a stream

SYNOPSIS
       #include <stdio.h>

       int setvbuf(FILE *restrict stream, char *restrict buf, int type,
           size_t size);

DESCRIPTION
       The functionality described on this reference page is aligned with
       the ISO C standard. Any conflict between the requirements described
       here and the ISO C standard is unintentional. This volume of
       POSIX.1?2008 defers to the ISO C standard.

       The setvbuf() function may be used after the stream pointed to by
       stream is associated with an open file but before any other operation
       (other than an unsuccessful call to setvbuf()) is performed on the
       stream. The argument type determines how stream shall be buffered, as
       follows:

        *  {_IOFBF} shall cause input/output to be fully buffered.

        *  {_IOLBF} shall cause input/output to be line buffered.

        *  {_IONBF} shall cause input/output to be unbuffered.
Run Code Online (Sandbox Code Playgroud)


Ces*_*arB 6

看看setbuf()和setvbuf().