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

#include <iostream>
#include <string>
#include <cstdio>
#include <memory>
#include <stdexcept>
#include <array>

std::string executeCommand(const char* cmd) {
    const int bufferSize = 128;
    char buffer[bufferSize];
    std::string result = "";
    std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);

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

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

    return result;
}

int main() {
    const char* command = "your_command_here";

    try {
        std::string output = executeCommand(command);
        std::cout << "Command Output:\n" << output << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Exception: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}