Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <array>
- #include <memory>
- std::string exec(std::string & cmd) {
- std::array<char, 128> buffer;
- std::string result;
- std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
- if (!pipe) {
- throw std::runtime_error("popen() failed!");
- }
- while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
- result += buffer.data();
- }
- return result;
- }
- int main (int argc, char ** argv) {
- if (std::atoi(argv[1]) == 0)
- return std::cout << 0, 0;
- std::string c = std::string() + argv[0] + " " + std::to_string(std::atoi(argv[1]) - 1);
- std::cout << 1 + std::stoi(exec(c));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement