📅  最后修改于: 2022-03-11 14:44:55.559000             🧑  作者: Mango
#include
#include
#include
#include
#include
#include
std::string exec(const char* cmd) {
std::array buffer;
std::string result;
std::unique_ptr 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();
}
return result;
}