Mihran Hovsepyan
Mihran Hovsepyan

Reputation: 11108

How to merge multiple file descriptors into the one?

In my C++ application I use some call to 3rd party lib for creating a new child process. I'm passing 2 FILE** variables to it and they being filled by pointers of stdout and stderr handlers of the child process. But in fact I don't need to read from them separately in separate threads, I just need to merge them into the one and read from there. How can I do that (both linux and Windows)?

Upvotes: 0

Views: 1778

Answers (2)

luser droog
luser droog

Reputation: 19504

Use dup2 (manpage).

#include <stdio.h>
#include <unistd.h>

dup2(fileno(stdout), fileno(stderr));

Upvotes: 2

ibi0tux
ibi0tux

Reputation: 2629

A file descriptor is linked by the system to a physic file. Merging two files descriptor into one would lead to get only one file.

If you always have those two file descriptors together, you can simply create a struct that would handle the two files descriptor in only one variable pointer.

Upvotes: 0

Related Questions