c 2b 2b start process and get output

Solutions on MaxInterview for c 2b 2b start process and get output by the best coders in the world

showing results for - "c 2b 2b start process and get output"
Jessim
07 Oct 2019
1#include <cstdio>
2#include <iostream>
3#include <memory>
4#include <stdexcept>
5#include <string>
6#include <array>
7
8std::string exec(const char* cmd) {
9    std::array<char, 128> buffer;
10    std::string result;
11    std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
12    if (!pipe) {
13        throw std::runtime_error("popen() failed!");
14    }
15    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
16        result += buffer.data();
17    }
18    return result;
19}
20