在 **可移植 C** 中,如何启动将命令的标准输入连接到启动器的标准输出的命令?

发布于 2024-07-07 09:08:43 字数 483 浏览 8 评论 0原文

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

请注意:

  1. 除此 stdout 之外的方法 --> stdin 管道也可以提供可移植跨 Windows 和 Linux。

  2. 我无法使用 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:

  1. A method other than this stdout -->
    stdin piping is also OK provided
    it is PORTABLE across Windows and
    Linux.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲性难收 2024-07-14 09:08:43

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

The Microsoft C runtime calls it _popen instead of popen, but it appears to have the same functionality in Windows (for console applications) and Linux.

绿光 2024-07-14 09:08:43

老实说,我并不是 100% 清楚你到底想实现什么目标。

但据我了解,你可以看看 Boost.Process

你可以这样做然后

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

使用 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

 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.

Anyway, an awful lot can be achieved with the library w.r.t. pipe redirecting and chaining.

眼眸印温柔 2024-07-14 09:08:43

链接的 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.

半世蒼涼 2024-07-14 09:08:43

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文