2010-12-13 75 views
1

例如以實現窗口的linux管道,在linux以下命令如何用C C++

$ firstProgram | secondProgram 

攜帶firstProgram的輸出作爲輸入提供給secondProgram

基本碼用C,使它發生在Linux中是

#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中做類似的事情。 謝謝

回答

3

請參閱Win32 CreatePipe()創建一個匿名管道。 This example(標題爲「使用重定向輸入和輸出創建子進程」)顯示如何在Win32中複製代碼。