How do I get shell output from system()?

帅比萌擦擦* 提交于 2021-01-29 01:31:53

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/20362013/how-do-i-get-shell-output-from-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!