Skip to content
KerwinKoo edited this page Dec 29, 2015 · 1 revision

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.

[[TOC]]

Clone this wiki locally