Running a child process in C++
Try to follow ruby pattern.
You want to capture stdout as a string (and inherit stderr):
from How to execute a command and get the output of command within C++ using popen:
NOTE: stderr not inherited with code below.
std::string exec(std::string command) {
char buffer[128];
std::string result;
// Open pipe to file
FILE* pipe = popen(command.c_str(), "r");
if (!pipe) {
return "popen failed!";
}
// read till end of process:
while (!feof(pipe)) {
// use buffer to read and add to result
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
Written on September 9, 2020, Last update on September 9, 2020
c++
shell
process