简体   繁体   English

如何将多个文件描述符合并为一个?

[英]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. 在我的C ++应用程序中,我使用对3rd party lib的一些调用来创建新的子进程。 I'm passing 2 FILE** variables to it and they being filled by pointers of stdout and stderr handlers of the child process. 我正在向其传递2个FILE**变量,它们由子进程的stdoutstderr处理程序的指针填充。 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)? 我该怎么做(Linux和Windows)?

Use dup2 ( manpage ). 使用dup2manpage )。

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

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

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. 如果您始终将这两个文件描述符一起使用,则可以简单地创建一个仅在一个变量指针中处理两个文件描述符的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM