开发者

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

开发者 https://www.devze.com 2023-04-11 22:59 出处:网络
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.

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

精彩评论

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