📜  c++ 启动进程并获取输出 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:55.559000             🧑  作者: Mango

代码示例1
#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;
}