#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.
result += buffer
, so the pipe might not be properly closed. – larsmans May 19 '12 at 20:27bad_alloc
. – larsmans Mar 14 at 22:00