在 **可移植 C** 中,如何启动将命令的标准输入连接到启动器的标准输出的命令?
在 C 程序 (p1) 中,如何启动动态构造的命令(及其参数)从 p1 的标准输出读取其标准输入?
请注意:
除此 stdout 之外的方法 --> stdin 管道也可以提供 它可移植跨 Windows 和 Linux。
我无法使用 C++、Java、Perl、Ruby、 Python 等。
另外,它的 Windows 版本是否有 MinGW 依赖性?
重新开放:下面的问题针对 Linux 回答了这个问题,但是这个问题需要一个可移植的方法。 从 C 程序中执行程序
In a C program (p1), how to launch a dynamically constructed command (and its arguments) that reads its standard input from p1's standard output?
Note that:
A method other than this stdout -->
stdin piping is also OK provided
it is PORTABLE across Windows and
Linux.I cannot use C++, Java, Perl, Ruby,
Python, etc here.
Also, will this have a MinGW dependency for its Windows build?
REOPENED: The question below answers it for Linux, but this question wants a portable method.
Execute program from within a C program
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Microsoft C 运行时将其称为
_popen
而不是popen
,但它似乎具有相同的Windows(用于控制台应用程序)和Linux 中的功能。The Microsoft C runtime calls it
_popen
instead ofpopen
, but it appears to have the same functionality in Windows (for console applications) and Linux.老实说,我并不是 100% 清楚你到底想实现什么目标。
但据我了解,你可以看看 Boost.Process
你可以这样做然后
使用 os 作为任何流将内容转储到子进程的标准输入中。
不管怎样,通过管道重定向和链接库可以实现很多功能。
It's not 100% clear to me what you're trying to achieve exactly to be honest.
But as I understand it, you could take a look at Boost.Process
You can do things like
And then use the os as any stream to dump stuff in the standard input of your child process.
Anyway, an awful lot can be achieved with the library w.r.t. pipe redirecting and chaining.
链接的 anser(您拒绝的)指的是 POSIX 管道。 POSIX 是 C 的扩展,它添加了标准 C 所缺少的功能。POSIX 管道就是此类功能的一个很好的例子:它们被添加到 POSIX 中是因为标准 C 没有该功能。
The linked anser (which you reject) refers to POSIX pipes. POSIX is an extension to C, which adds features missing from standard C. POSIX pipes are a good example of such a feature: they were added to POSIX because standard C does not have that functionality.
glib 库是用 C 语言编写的,其实现在 Linux 和 Windows 上都经过了良好的测试。 有关生成新进程的手册部分将为您提供以下信息:如何使用 g_spawn_async_with_pipes 调用启动新进程并挂钩其 stdin 和 stdout 句柄。
The glib library is written in C and has implementations that are well tested on both Linux and Windows. The manual section on spawning new processes will give you information about how to start a new process and hook into its stdin and stdout handles using the g_spawn_async_with_pipes call.