Passing System Call Output Into A Variable

Dear all,

Is there a way to do it with C++?

Typically in Perl this could be done
this way:

1
2
3
my @output = `echo foo \n echo bar`;
chomp (@output);
print "$output[0] $output[1]\n";


I am wondering what's the C++ idiom
for the above task.
Last edited on
No, not directly/easily.

You have to create a pipe that connects stdout of the target program to a file descriptor in your program that you can read.

Or else redirect the output of your target program to a file and then have your program read the file.
Well if i have understood your problem then this may be the solution.


#include <iostream>
using namespace std;

int (*ptr) (const char[]);

int main()
{
ptr = system;


....... procedure ......
....... work .......


ptr("clear");
return 0;
}

system is a function to pass commands to the shell. You can make a pointer to the function and call it just as a function with the command as the argument. So you have captured the command using an identifier.
You can also read from stdin. If for example, you wanted to run a program and pipe its output into your program...
Topic archived. No new replies allowed.