how to save system function output into a variable in c++

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <array>

int main() {
    std::array<char, 128> buffer;
    FILE* pipe = popen("system_command_here", "r");
    if (!pipe) {
        return EXIT_FAILURE;
    }

    std::string result;
    while (fgets(buffer.data(), buffer.size(), pipe) != nullptr) {
        result += buffer.data();
    }

    pclose(pipe);

    // Now 'result' contains the output of the system command
    return EXIT_SUCCESS;
}