Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #execute #command #return #code #stdout #stderr #command
ADD COMMENT
Topic
Name
9+3 =