How can I use fprintf and write to pipe?

I created a channel and I used dup2 () to rewrite streams 1 and 2 (stdout and stderr) to these channels.

Now I want to use fprintf to write to stream 1 or 2, but my program does not seem to get anything on the other side of the pipe. I tried using printf (), but I'm not sure if this is written to stdout or stream 1 by default. If it writes to stream 1, I think its problem is somewhere deeper in my code.

Essentially, I ask, given the int representing the stream, how can I get FILE * suitable for use in fprintf ()?

+7
c printf pipe system
source share
1 answer

If you have a file descriptor and want FILE* , you can use fdopen

 FILE *fdopen(int fd, const char *mode); 

fdopen is a Posix function and is documented in man fdopen . To do the opposite, you can use fileno

+6
source share

All Articles