gpt4 book ai didi

在c/c++中调用多个popen

转载 作者:行者123 更新时间:2023-11-30 19:22:10 29 4
gpt4 key购买 nike

我使用 FreeBSD 和 C/C++ 来控制机器人。问题是这样的:

我在写入模式下使用popen()向计算机发出命令以控制机器人。所以现在我想获取命令的输出,因此我在阅读模式下创建另一个 popen() 来获取它。

它会起作用吗?

我应该创建某种父进程和子进程吗?

最佳答案

So now I want to get the output of the command so I create another popen() in reading mode to get it.

不,使用 popen(3) 创建附加流将不起作用。然而,由于您使用的是 FreeBSD,所以您很幸运:在 FreeBSD 上,管道是双向的,因此 popen(3) 接受 r+ 参数。来自 the manual :

Since popen() is now implemented using a bidirectional pipe, the type argument may request a bidirectional data flow.The type argument is a pointer to a null-terminated string which must be 'r' for reading, 'w' for writing, or 'r+' for reading and writing.

因此,您可能只需使用 fgets(3) 之类的内容从同一 FILE * 读取输出即可。

关于在c/c++中调用多个popen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18079732/

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