1

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.

Mike
  • 963
  • 4
  • 16
  • 38
  • you can get this from following links http://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c http://stackoverflow.com/questions/14393762/executing-bash-command-and-getting-the-output-in-c – reegan vijay Dec 04 '13 at 05:40

1 Answers1

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);
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Great, thanks. Do you think you can point me in the right direction for reading the file into a string? – Mike Dec 03 '13 at 21:40