Given the following answer (first c++11 answer):
How do I execute a command and get the output of the command within C++ using POSIX?
Here is the implementation for your convenience:
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
result += buffer.data();
}
return result;
}
This works really nicely to execute a command (e.g. std::string res = exec("ls");
) and get the stdout into a string.
But what it does not do is get the command return code (pass/fail integer) or the stderr. Ideally I would like a way to get all three (return code, stdout, stderr).
I would settle for stdout and stderr. I am thinking that I need to add another pipe, but I can't really see how the first pipe is setup to get stdout so I can't think how I would change it to get both.
Any one got any ideas how to do that, or alternative approaches that may work?
update
See my complete example here with the output:
Start
1 res: /home
2 res: stdout
stderr
3 res:
End
You can see that 3 res:
does not print stderr in the same way that 2 res: stdout
does, but stderr is just dumped onto the screen on a separate line by the process (and not my program).
External Libs
I really don't want to use external libraries like Qt and boost - mostly because I want the portability of it and also many projects that I work on don't use boost. However I will mark up solutions that contain these options as they are valid for other users :)
Complete Solution Using Comments/Answer
Thanks all for your answers / comments, here is the modified solution (and runable):
working-solution
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…