如何在c ++中重定向输出读取bash脚本?

1 c++ bash redirect stdout

我知道这个功能:

system("myfile.sh")
Run Code Online (Sandbox Code Playgroud)

执行一个bash脚本.好的,但现在我想将输出重定向到我的程序以确保读数.例如脚本date.sh给我系统的日期,我希望在我的程序中用std :: cout << OUTPUTDATE; 可能吗?怎么样?

cni*_*tar 6

popen而不是system.

该功能popen将为您提供一个FILE *可以阅读的功能.

FILE *script = popen("myfile.sh", "r");
while (fgets(line, LENGTH, script)) {
    /* ... */
}
pclose(script);
Run Code Online (Sandbox Code Playgroud)