问题
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