execute script with C++ [duplicate]

空扰寡人 提交于 2019-12-13 08:37:45

问题


I want to execute a script through a c++ program and get its output. Presently I am doing

system("./script.sh > out.txt");

But I need a command that get the output to a string, some thing like:

out = system("./script.sh");
printf(out);

I can't read the file out.txt after execute the script because I don't have permission to that. I deployed my c++ program at other framework (boinc) that doesn't give me this permission.

Does anybody have a hint? Thanks in advance! Felipe


回答1:


you can use popen() and then get the output of the command from the pipe opened by popen()

FILE  *fp;
fp=popen("./script.sh","r");

and to get your output. you can use fgets() or fread() to read from pipe like you read from a file



来源:https://stackoverflow.com/questions/16235370/execute-script-with-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!