c++调用系统指令并获取指令输出
函数定义
std::string cmdResult() {
const char* cmd = "python3 test.py";
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
if(!result.empty())
return result ; //.substr(0, 33)
else
return "vslam";
}
函数使用
std::string cmd_result = AlgCross::getLastestGitInfo();
cout<<"##############result: "<<cmd_result<<std::endl;