Force line-buffering of stdout when piping to tee
Posted
by
houbysoft
on Stack Overflow
See other posts from Stack Overflow
or by houbysoft
Published on 2012-07-05T01:58:44Z
Indexed on
2012/07/05
3:16 UTC
Read the original article
Hit count: 259
Usually, stdout
is line-buffered. In other words, as long as your printf
argument ends with a newline, you can expect the line to be printed instantly. This does not appear to hold when using a pipe to redirect to tee
.
I have a C++ program, a
, that outputs strings, always \n
-terminated, to stdout
.
When it is run by itself (./a
), everything prints correctly and at the right time, as expected. However, if I pipe it to tee
(./a | tee output.txt
), it doesn't print anything until it quits, which defeats the purpose of using tee
.
I know that I could fix it by adding a fflush(stdout)
after each printing operation in the C++ program. But is there a cleaner, easier way? Is there a command I can run, for example, that would force stdout
to be line-buffered, even when using a pipe?
© Stack Overflow or respective owner