I'm writing a program that needs to call a shell command during runtime. The output of said command needs to come back into the program as a c++ string.
At first I just used
Code:
system("echo Hello World > temp");
string hello = getFile("temp");//where getFile simply returns the contents of temp as a string.
However, this is horrendously slow and isn't practical. I want to redirect the output from shell directly into my program. I attempted
Code:
system("echo Hello World 1>&0");
string hello;
while(!cin.eof()){
char c;
cin.get(c);
hello += c;
}
but this failed also.
If anyone can help, it would be greatly appreciated. I have a basic knowledge of C++ but only a rough understanding of bash shells, and so may be missing something obvious.