Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <string>
- std::string exec(const char* cmd, const std::string &input) {
- FILE *input_pipe = popen(cmd, "w");
- FILE *output_pipe = popen(cmd, "r");
- if (!output_pipe || !input_pipe) {
- throw std::ios_base::failure("invalid command");
- }
- fputs(input.c_str(), input_pipe);
- pclose(input_pipe);
- char buffer[128];
- std::string result = "";
- while (!feof(output_pipe)) {
- if (fgets(buffer, 128, output_pipe) != NULL) {
- result += buffer;
- }
- }
- pclose(output_pipe);
- return result;
- }
- int main() {
- const char *cmd = "./other";
- std::cout << "output is \n" << exec(cmd, "3\n2 4 5");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement