Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <array>
  4. #include <memory>
  5.  
  6. std::string exec(std::string & cmd) {
  7.     std::array<char, 128> buffer;
  8.     std::string result;
  9.     std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
  10.     if (!pipe) {
  11.         throw std::runtime_error("popen() failed!");
  12.     }
  13.     while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
  14.         result += buffer.data();
  15.     }
  16.     return result;
  17. }
  18.  
  19. int main (int argc, char ** argv) {
  20.     if (std::atoi(argv[1]) == 0)
  21.         return std::cout << 0, 0;
  22.     std::string c = std::string() + argv[0] + " " + std::to_string(std::atoi(argv[1]) - 1);
  23.     std::cout << 1 + std::stoi(exec(c));
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement