Last active
February 17, 2018 13:52
-
-
Save dwburke/5081cef85e12a1854dccdc27587ee517 to your computer and use it in GitHub Desktop.
Pipe :: Class for running a system command and capturing output, automatically closing when it goes out of scope
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
auto pipe = std::make_shared<Pipe>(); | |
auto pfp = pipe->open(argument, "r"); | |
while (fgets(syscom, 120, pfp) != NULL) | |
cout << syscom; |
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
class Pipe { | |
public: | |
Pipe() {}; | |
Pipe(char *command, char *mode) { | |
open(command, mode); | |
}; | |
~Pipe() { | |
if (pfp) | |
pclose(pfp); | |
}; | |
FILE *open(char *command, char *mode) { | |
if (!(pfp = popen(command, mode))) | |
throw Exception("Error opening pipe"); | |
return pfp; | |
}; | |
FILE *fhandle() { return pfp; }; | |
private: | |
FILE *pfp = nullptr; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment