views:

277

answers:

3

I have set up pipes to redirect stderr and stdout. When I use printf, does it send data to stdout or to stream 1?

If it sends it to stdout, how can I instead configure it to send data to stream 1?

+3  A: 

When I use printf, does it send data to stdout or to stream 1?

The printf function sends data to stdout. Since you have redirected stdout it now points to a file other than your console. printf doesn't care what it is, it just keeps writing as long as it can.

If it sends it to stdout, how can I instead configure it to send data to stream 1?

So, no you don't need to do anything special -- it is taken care of automatically.

dirkgently
+3  A: 

printf always sends data to stdout. If you have redirected stdout using a pipe, it will go to the process that stdout is being piped to.

And stream 1 (assuming you mean the stream associated with file descriptor 1) is stdout, unless you are doing something really weird (in which case, stop)

David Zaslavsky
+2  A: 

I think your understanding of the layers involved is a little bit misguided.

The C runtime's printf uses FILE *stdout, which (unless you use freopen or similar) is effectively equivalent to fdopen(1, "w"): that is, buffered output on file descriptor #1.

Normally, when you run my_program, file descriptor #1 is attached to the terminal. When you run my_program > file, file descriptor #1 is attached to file before your program even starts running. So printf, writing to stdout, which (unless you change it) scribbles to file descriptor #1, will print to the file and not the terminal.

Nothing outside of your program cares about your buffered I/O or what you do with your own stdout.

ephemient