简体   繁体   English

在** portable C **中,如何启动将命令的stdin连接到启动器的stdout的命令?

[英]In **portable C**, how to launch a command connecting the command's stdin to the launcher's stdout?

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? 在C程序(p1)中,如何启动动态构造的命令(及其参数),从p1的标准输出读取其标准输入?

Note that: 注意:

  1. A method other than this stdout --> stdin piping is also OK provided it is PORTABLE across Windows and Linux. 除了这个stdout - > stdin管道之外的方法也可以, 只要它在Windows和Linux上是PORTABLE

  2. I cannot use C++, Java, Perl, Ruby, Python, etc here. 我不能在这里使用C ++,Java,Perl,Ruby,Python等。

Also, will this have a MinGW dependency for its Windows build? 此外,它的Windows版本是否具有MinGW依赖性?

REOPENED : The question below answers it for Linux, but this question wants a portable method. REOPENED :下面的问题为Linux解决了这个问题,但这个问题需要一个可移植的方法。 Execute program from within a C program 从C程序中执行程序

Microsoft C运行时将其称为_popen而不是popen ,但它似乎在Windows(用于控制台应用程序)和Linux中具有相同的功能。

It's not 100% clear to me what you're trying to achieve exactly to be honest. 对我而言,这并不是100%清楚你要实现的目标。

But as I understand it, you could take a look at Boost.Process 但据我所知,你可以看看Boost.Process

You can do things like 你可以做的事情

 bp::child cs = p.start();
 bp::postream& os = cs.get_stdin();

And then use the os as any stream to dump stuff in the standard input of your child process. 然后使用os作为任何流来转储子进程的标准输入中的内容。

Anyway, an awful lot can be achieved with the library wrt pipe redirecting and chaining. 无论如何,使用管道重定向和链接的库可以实现很多。

The linked anser (which you reject) refers to POSIX pipes. 链接的anser(您拒绝)是指POSIX管道。 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. POSIX是C的扩展,它增加了标准C中缺少的功能.POSIX管道是这种功能的一个很好的例子:它们被添加到POSIX,因为标准C没有该功能。

The glib library is written in C and has implementations that are well tested on both Linux and Windows. glib库是用C语言编写的,并且具有在Linux和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. 产生新进程的手册部分将为您提供有关如何使用g_spawn_async_with_pipes调用启动新进程并挂接到其stdin和stdout句柄的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM