C equivalent of autoflush (flush stdout after each write)?

前端 未结 3 583
心在旅途
心在旅途 2020-12-06 08:59

In Perl, I can type:

$|++;

and anything printed to STDOUT will be automatically fflush()ed.

Is there an equivalent in C? In other w

相关标签:
3条回答
  • 2020-12-06 09:33

    Take a look at setbuf() and setvbuf().

    0 讨论(0)
  • 2020-12-06 09:40

    Try setvbuf(stdout, NULL, _IONBF, 0). It changes stdout to unbuffered (_IONBF) mode.

    0 讨论(0)
  • 2020-12-06 09:53

    I haven't done this, but _IOLBF would be the right answer.

    $ 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.
    
    0 讨论(0)
提交回复
热议问题