Horje
How to execute a command and get return code stdout and stderr of command in C++ Code Example
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;
}




Cpp

Related
pain Code Example pain Code Example
stack implementation Code Example stack implementation Code Example
the number of ones int bitset Code Example the number of ones int bitset Code Example
c create 1 bit value Code Example c create 1 bit value Code Example
access last element of set c++ Code Example access last element of set c++ Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9