当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - How do I get shell output from system()?

问题描述:

I'm calling system() to trigger a command. I can see the output of the command in the Xcode console – but I don't know how to capture it in a string.

I tried setting a string to the system() call itself, but the string was set to 0.

This is the code I wrote:

string node = "/usr/local/bin/node ~/Desktop/chromix-master/script/chromix.js ";

string commandStr = node + "url";

char command[1024];

strcpy(command,commandStr.c_str());

system(command);

Specifically, I'm trying to get the URL of the currently focused tab in chrome using smblott's Chromix utility.

网友答案:

Instead of using system(), use popen() to open a pipe from which you can read the program output.

FILE *p = popen(command, "r");
// ... use p as a file
pclose(p);
分享给朋友:
您可能感兴趣的文章:
随机阅读: