On Tue, Jul 12, 2005, Alon Altman wrote about "Strange C-code behavior with pipes": > When I compile this into a file called "prog" and then run: "./prog|cat" I > see that "Hello" is not printed until I give it input. Where is the buffer > here and is there any way to bypass it?
Most people know, or at least assume, that stdio makes "stderr" unbuffered, and "stdout" line buffered, i.e., whenever you print a line to stdout it gets shown. Well, this is almost true. It is true if the standard output is a terminal (see isatty(3)). However, if the standard output is *not* a terminal - i.e., it is redirected to a file or a pipe - stdout becomes fully buffered like any other file. This is why your a.out showed it's output immediately, while a.out | cat, or a.out > file, buffers its output. This is actually explained in the setbuf(3) manual page, and this function (and its relatives) is the one you can use to force line-buffered behavior on your stdout always, regardless of where the output is going. You can also use fflush(stdout) explicitly after the write, to make sure it gets flushed. -- Nadav Har'El | Tuesday, Jul 12 2005, 5 Tammuz 5765 [EMAIL PROTECTED] |----------------------------------------- Phone +972-523-790466, ICQ 13349191 |Microchips: what's left at the bottom of http://nadav.harel.org.il |the bag when it reaches you. ================================================================= To unsubscribe, send mail to [EMAIL PROTECTED] with the word "unsubscribe" in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]