boost提供了boost::process::child,可以通过其调用其他程序,并获得输出:
- #include
- #include
- #include
- #include
- #include
- #include
-
- using namespace std;
-
- template <typename... ArgTypes>
- tuple<int, vector
> execCmd(const char* path, ArgTypes&&... tArgs) - {
- vector
stdOutput; - boost::process::ipstream stdOutStream;
- boost::process::child execProg(path, const_cast<char*>(tArgs)...,
- boost::process::std_out > stdOutStream);
- string stdOutLine;
-
- while (stdOutStream && getline(stdOutStream, stdOutLine) && !stdOutLine.empty())
- {
- stdOutput.emplace_back(stdOutLine);
- }
-
- execProg.wait();
-
- int retCode = execProg.exit_code();
- return make_tuple(retCode, stdOutput);
- }
-
- int main()
- {
- int cmdRetC