Created
January 28, 2015 01:30
-
-
Save lethee/3e1686715539483024ea to your computer and use it in GitHub Desktop.
popen example: exec shell command output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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