Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中从外部程序捕获stderr和stdout+;_C++_Linux - Fatal编程技术网

C++ 在C+中从外部程序捕获stderr和stdout+;

C++ 在C+中从外部程序捕获stderr和stdout+;,c++,linux,C++,Linux,我正在尝试编写一个运行外部程序的程序 我知道我可以捕捉到stdout,我可以同时捕捉到stdout和stderr,但问题是我可以捕捉到stderr和stdout分开的吗 我的意思是,例如,变量stderr中的stderr和变量stdout中的stdout。我是说我想让他们分开 此外,我还需要一个变量中的外部程序的退出代码。在Windows上,您必须填写STARTUPINFO,以便CreateProcess捕获标准流,并且您可以使用GetExitCodeProcess函数获取终止状态。下面是一个如

我正在尝试编写一个运行外部程序的程序

我知道我可以捕捉到
stdout
,我可以同时捕捉到
stdout
stderr
,但问题是我可以捕捉到
stderr
stdout
分开的吗

我的意思是,例如,变量
stderr
中的
stderr
和变量
stdout
中的
stdout
。我是说我想让他们分开


此外,我还需要一个变量中的外部程序的退出代码。

在Windows上,您必须填写
STARTUPINFO
,以便
CreateProcess
捕获标准流,并且您可以使用
GetExitCodeProcess
函数获取终止状态。下面是一个如何将standart流重定向到父进程的示例

在类似Linux的操作系统上,您可能希望使用
fork
而不是
execve
,而使用fork进程则是另一回事

在Windows和Linux中,重定向流有一种通用的方法——您必须创建几个管道(每个管道一个),并将子进程流重定向到该管道中,父进程可以从该管道读取数据

Linux的示例代码:

int fd[2];

if (pipe(fd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
}

pid_t cpid = fork();
if (cpid == -1) {
    perror("fork");
    exit(EXIT_FAILURE);
}

if (cpid == 0) { // child
    dup2(fd[1], STDERR_FILENO);
    fprintf(stderr, "Hello, World!\n");
    exit(EXIT_SUCCESS);
} else { // parent
    char ch;
    while (read(fd[0], &ch, 1) > 0)
        printf("%c", ch);
    exit(EXIT_SUCCESS);
}
编辑:如果您需要捕获来自另一个程序的流,请使用与上面相同的策略,首先使用
fork
,其次使用管道(如上面的代码所示),然后在子进程中使用
execve
另一个程序,并在父进程中使用此代码等待执行结束并捕获返回代码:

int status;
if (waitpid(cpid, &status, 0) < 0) {
    perror("waitpid");
    exit(EXIT_FAILURE);
}
int状态;
if(waitpid(cpid,&status,0)<0){
佩罗(“waitpid”);
退出(退出失败);
}

您可以在手册页和中找到更多详细信息。

什么操作系统?你知道用什么方法把它们结合在一起吗?我正在使用Ubuntu,我知道我可以使用2>&1。但我希望他们分开。谢谢你的回答: