popen - KerwinKoo/KerwinKoo.github.io GitHub Wiki

C++ popen

获取命令全部返回信息

#include <string>
#include <iostream>
#include <stdio.h>

std::string exec(char* cmd) {
    FILE* pipe = popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    std::string result = "";
    while(!feof(pipe)) {
    	if(fgets(buffer, 128, pipe) != NULL)
    		result += buffer;
    }
    pclose(pipe);
    return result;
}
Replace popen and pclose with _popen and _pclose for Windows.
⚠️ **GitHub.com Fallback** ⚠️