开发者

How to redirect output from reading a bash script in c++?

I know that the function:

system("myfile.sh")

exec a bash script. Ok but now I want to redirect the开发者_如何学Python output to my program to ensure the reading. For example the script date.sh give me the date of my system, and i want to see it on my program with std::cout << OUTPUTDATE; Is it possible? How?


Use popen instead of system.

The function popen will give you a FILE * you can read from.

FILE *script = popen("myfile.sh", "r");
while (fgets(line, LENGTH, script)) {
    /* ... */
}
pclose(script);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜