我使用下面的代码来提取系统命令的输出。我没有在path变量中设置pic的PATH。我想存储命令"which pic"的输出,而不想在控制台上显示它。
下面是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main ()
{
FILE *fp;
int status;
char path[1035];
char *command = "which pic";
/* Open the command for reading. */
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(0);
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path)-1, fp) != NULL) {
cout<<"<<<<<<<<<<,"<<endl;
printf("%s", path);
}
/* close */
pclose(fp);
return 0;
}但它在控制台中显示以下输出:
which: no pic in(/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin)发布于 2013-01-03 18:30:52
按照您的命令运行"which pic 2>&1"。您希望捕获来自which的所有输出,包括它的错误(发送到stderr)。
https://stackoverflow.com/questions/14135554
复制相似问题