Skip to content

Instantly share code, notes, and snippets.

@dwburke
Last active February 17, 2018 13:52
Show Gist options
  • Save dwburke/5081cef85e12a1854dccdc27587ee517 to your computer and use it in GitHub Desktop.
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
auto pipe = std::make_shared<Pipe>();
auto pfp = pipe->open(argument, "r");
while (fgets(syscom, 120, pfp) != NULL)
cout << syscom;
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