例如,在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/