【发布时间】:2017-06-21 15:21:29
【问题描述】:
我正在通过 posix popen() 函数打开一个进程。例如。 git push、mkdir x 等
我可以通过将这些命令的输出存储到这样的缓冲区中轻松读取:
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
FILE *in;
char buff[512];
if(!(in = popen("mkdir x", "r"))){
return 1;
}
// fgets stores the output into buff
while(fgets(buff, sizeof(buff), in)!=NULL){
cout << buff;
}
pclose(in);
return 0;
}
但是,如果过程出现错误,例如mkdir 失败,然后我想将错误读入字符串或字符缓冲区。
但是,对于上面的代码,如果失败,错误不会存储在缓冲区中。我认为是因为错误被重定向到标准错误而不是标准输入。
如何修改上面的代码得到bash/进程返回的错误信息?
【问题讨论】:
标签: c++