2013-08-28 40 views
1

我想將pipe的寫入fd關聯到stdout。如何更改與stdin文件描述符關聯的fd?

int pfds[2]; 
char buf[30]; 

if (pipe(pfds) == -1) { 
    perror("pipe"); 
    exit(1); 
} 

I want to associate pfd[1] to the stdout of the process. 

據我所知,我們可以使用freopen將stdout重定向到文件。我希望能得到類似的東西。

+0

爲什麼不'PFD [1] = stdout',然後呢? –

回答

3

dup2(2)可能是最簡單的方法:

dup2(pfds[1], STDOUT_FILENO); 
+0

你很可能想關閉原始管道描述符,關閉(pfds [1]) – nos

+0

Yup;這是可能的。 –

相關問題