/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <cstdio>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
#include <array>
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (!feof(pipe.get())) {
if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
result += buffer.data();
}
return result;
}
using namespace std;
int main()
{
cout<<"Start\n";
std::string res;
res = exec("pwd");
std::cout << "1 res: " << res << std::endl;
res = exec("echo stdout >&1");
std::cout << "2 res: " << res << std::endl;
res = exec("echo stderr >&2");
std::cout << "3 res: " << res << std::endl;
cout<<"End\n";
return 0;
}