gpt4 book ai didi

c - Unix 和 C 中的输出重定向

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:08 25 4
gpt4 key购买 nike

我正在尝试使用 system() 命令在我的 C 程序中运行脚本。在 main() 中,我运行脚本并返回结果。如何将脚本的结果放入某个字符串中并检查条件?我知道我可以用文件做到这一点,但想知道是否可以将结果放入字符串中。

示例如下:

main()
{
system("my_script_sh"); // How can I get the result of the my_script_sh
}

最佳答案

你不能为此使用系统命令。最好的办法是使用 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);

关于c - Unix 和 C 中的输出重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5629988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com