#include #include std::string execute(const std::string& command) { system((command + " > temp.txt").c_str()); std::ifstream ifs("temp.txt"); std::string ret{ std::istreambuf_iterator(ifs), std::istreambuf_iterator() }; ifs.close(); // must close the inout stream so the file can be cleaned up if (std::remove("temp.txt") != 0) { perror("Error deleting temporary file"); } return ret; } int main() { std::cout << execute("whoami") << '\n'; }