Question

In Perl, I can type:

$|++;

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

Is there an equivalent in C? In other words, is there some way I can tell stdio to automatically fflush stdout after every printf(), the way it automatically flushes stderr?

Was it helpful?

Solution

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

OTHER TIPS

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 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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top