我正在尝试使用system()命令在我的C程序中运行脚本.在里面main(),我运行脚本并返回结果.如何将脚本的结果放在某个字符串中并检查条件?我知道我可以用文件来做,但想知道是否可以将结果放入字符串中.
样本如下:
main()
{
system("my_script_sh"); // How can I get the result of the my_script_sh
}
Run Code Online (Sandbox Code Playgroud)
您无法使用系统命令.最好的办法是使用popen:
FILE *stream;
char buffer[150];
stream = popen("ls", "r");
while ( fgets(buffer, 150, stream) != NULL ){
// Copy the buffer to your output string etc.
}
pclose(stream);
Run Code Online (Sandbox Code Playgroud)