Skip to content

Instantly share code, notes, and snippets.

@lethee
Created January 28, 2015 01:30
Show Gist options
  • Save lethee/3e1686715539483024ea to your computer and use it in GitHub Desktop.
Save lethee/3e1686715539483024ea to your computer and use it in GitHub Desktop.
popen example: exec shell command output
bool exec(const std::string &cmd, std::string* out)
{
FILE* pipe = popen(cmd.c_str(), "r");
if (pipe == NULL) {
return false;
}
char buffer[2048];
while (!feof(pipe)) {
if (fgets(buffer, sizeof(buffer), pipe) != NULL) {
out->append(buffer);
}
}
pclose(pipe);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment