32

Perl では、次のように入力できます。

$|++;

STDOUT に出力されたものはすべて自動的に fflush() されます。

Cに同等のものはありますか?つまり、すべての printf() の後で stderr を自動的にフラッシュする方法で、stdio に stdout を自動的にフラッシュするように指示できる方法はありますか?

4

3 に答える 3

42

試してみてくださいsetvbuf(stdout, NULL, _IONBF, 0)stdout非バッファ ( _IONBF) モードに変わります。

于 2008-10-18T00:45:41.117 に答える
14

私はこれを行っていません_IOLBFが、正しい答えです。

$ 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.
于 2008-10-18T00:46:06.077 に答える
6

setbuf() と setvbuf() を見てください。

于 2008-10-18T00:43:48.093 に答える