c++ - 如何使用C\C++在windows中实现linux管道

标签 c++ c process redirect io-redirection

例如,在linux中有如下命令

$ firstProgram | secondProgram

将第一个程序的输出作为第二个程序的输入

在 linux 中实现它的 C 基本代码是

#include <unistd.h>
.
.
.
int fd[2];
forkStatus = fork();
if (status == 0)
{
  close(1);
  dup(fd[1]);
  close(fd[1]);
  close(fd[0]);
  execv("firstProgram",...);
}
forkStatus = fork();
if (status == 0)
{
  close(0);
  dup(fd[0]);
  close(fd[1]);
  close(fd[0]);
  execv("secondProgram",...);
}
close(fd[1]);
close(fd[0]);

我需要在 Windows 中做类似的事情。 谢谢

最佳答案

参见 Win32 <a href="http://msdn.microsoft.com/en-us/library/aa365152%28v=VS.85%29.aspx" rel="noreferrer noopener nofollow">CreatePipe()</a>创建一个匿名管道。 This example (标题为“使用重定向的输入和输出创建子进程”)展示了如何在 Win32 中复制您的代码。

关于c++ - 如何使用C\C++在windows中实现linux管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4429127/

相关文章:

c - 数组中两个相邻值之间的最大差异,C

linux - 进程间读取不正确

c++ - 在定义自定义接口(interface)时继承库类

c++ - 在 C++ 中通过指针传递比通过引用传递有好处吗?

c++ - Openmp 程序在没有临界区的情况下工作

C# - 在新进程中执行静态方法

linux - 如何设置shell脚本的进程组

python - 发现倍数时被行为所困扰

C中的条件表达式

c - 从不同的指针 C 获取指向内存地址的指针