Search
 
SCRIPT & CODE EXAMPLE
 

CPP

How to execute a command and get return code stdout and stderr of command in C++

std::string exec(const char* cmd) {
    std::array<char, 128> buffer;
    std::string result;

    auto pipe = popen(cmd, "r"); // get rid of shared_ptr

    if (!pipe) throw std::runtime_error("popen() failed!");

    while (!feof(pipe)) {
        if (fgets(buffer.data(), 128, pipe) != nullptr)
            result += buffer.data();
    }

    auto rc = pclose(pipe);

    if (rc == EXIT_SUCCESS) { // == 0

    } else if (rc == EXIT_FAILURE) {  // EXIT_FAILURE is not used by all programs, maybe needs some adaptation.

    }
    return result;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: start google 
Cpp :: Print value of data in c++ 
Cpp :: find node from pos linkedlist c++ 
Cpp :: c++ click event 
Cpp :: what is blob in computer vision 
Cpp :: passing array to the function c++ 
Cpp :: vermífugo significado 
Cpp :: preorder to postorder converter online 
Cpp :: C++ singleton prevent copy 
Cpp :: Corong_ExerciseNo3(1) 
Cpp :: command loop ctrl D c++ 
Cpp :: c++ define function in header 
Cpp :: c++ start process and get output 
Cpp :: left margin c++ 
Cpp :: pop back innstring 
Cpp :: Passing a string to a function 
Cpp :: how to adjust and delete memory in c, c++ 
Cpp :: c+ 
Cpp :: c++ 
Cpp :: generate consecutive numbers at compile time 
Cpp :: convert preorder to postorder calculator 
Cpp :: sort n characters in descending order c++ 
Cpp :: c++ hsl to rgb integer 
Cpp :: racing horses codechef solution c++ 
Cpp :: temporary variable ex c++ 
Cpp :: font family slick 
Cpp :: QMetaObject_invokeMethod 
Cpp :: c++ coding questions for interview 
Cpp :: c++ max 
Cpp :: how to convert n space separated integers in c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =