gpt4 book ai didi

c - 在C程序中获取命令行结果

转载 作者:行者123 更新时间:2023-12-01 22:18:52 26 4
gpt4 key购买 nike

有没有办法读取 C 程序中命令行的结果?

例如,如果我在 C 程序中有以下代码

sprintf(command, "cat input_file.txt | wc -l");
system(command);

我可以获取 wc -l 的结果并将其存储在变量中,以便我可以在同一个 C 程序中使用它吗?

最佳答案

是的,请查看popen() ,如果你有的话。

一般来说,您必须生成一个子进程并设置一个管道来读取其标准输出。

关于c - 在C程序中获取命令行结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24651525/

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