Search
 
SCRIPT & CODE EXAMPLE
 

CPP

cpp execute command

//----------------------------------
//  CREDIT: gregpaton08 on stack overflow
//  https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po
//----------------------------------
#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::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
    if (!pipe) {
        throw std::runtime_error("popen() failed!");
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
    return result;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: linked list cycle c++ 
Cpp :: how to declare a vector of int in c++ 
Cpp :: print in c ++ 
Cpp :: install qpid broker in ubuntu/linux 
Cpp :: put text on oled 
Cpp :: how to check char array equality in C++ 
Cpp :: ue4 c++ replicate actor variable 
Cpp :: User defined functions and variables in C++ programming 
Cpp :: reference c++ 
Cpp :: c++ remove all elements equal to 
Cpp :: array copx c++ 
Cpp :: printing in column c++ 
Cpp :: cmd color text c++ 
Cpp :: linear search 
Cpp :: google test assert exception 
Cpp :: auto in c++ 
Cpp :: closing a ifstream file c++ 
Cpp :: c++ for loop syntax 
Cpp :: How to pass a multidimensional array to a function in C and C++ 
Cpp :: DSA 2. Complexity Analysis Google drive Educative excellent courses!!!! [Educative.io] Competitive Programming in C++ The Keys to Success 
Cpp :: find factorial in c++ using class 
Cpp :: C++ Taking Multiple Inputs 
Cpp :: javascript if else exercises 
Cpp :: C++ switch..case Statement 
Cpp :: linkedlist in c++ 
Cpp :: book allocation problem in c++ 
Cpp :: gtest assert not equal 
Cpp :: i++ i-- 
Cpp :: person parametr cpp 
Cpp :: initalising array c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =