2011-04-25 19 views
1

我確實有一個在HPUX和Linux上運行的C++程序。我的程序創建2個子進程,父進程等子進程完成。當我執行我的程序窗體運行目錄時,如下所示, run> myProgram在父級以編程方式從子進程捕獲打印,以便它們不會轉到標準輸出

我從顯示的子進程+父進程中獲取打印。所以我需要阻止我的子進程打印到命令提示符窗口。子進程完成後,我想打開打印,以便父級可以顯示結果。

有誰知道如何打開和關閉打印?

+1

如何你的孩子進程創建的?他們是來自你父母的分叉,還是他們是從系統命令產生的? – Dan 2011-04-25 20:56:06

+1

你真的想打開和關閉stdout嗎?你能不能簡單地將它們重定向到類似文件的東西,然後在完成後讀取它?或者那不是你想要的? – Bart 2011-04-25 21:03:12

回答

1

this answer獲得靈感:

#include <stdio.h> 

main() 
{ 
    int fd; 
    fpos_t pos; 

    printf("printing to stdout enabled\n"); 

    fflush(stdout); 
    fgetpos(stdout, &pos); 
    fd = dup(fileno(stdout)); 

    // Standard output redirected to the null device 
    freopen("/dev/null", "w", stdout); 

    f(); 

    // Standard output restored to its previous fd (the screen) 
    fflush(stdout); 
    dup2(fd, fileno(stdout)); 
    close(fd); 
    clearerr(stdout); 
    fsetpos(stdout, &pos);  /* for C9X */ 

    printf("printing to stdout enabled again\n"); 
} 

f() 
{ 
    printf("message sucked away by /dev/null"); 
} 
+0

非常感謝你 – usustarr 2011-04-25 23:30:42

+0

不客氣,記住也可以upvote接受/有用的答案! :) – 2011-04-25 23:31:56

+0

可悲的是我沒有足夠的投票來upvote u,對不起。你碰巧知道如何關閉打印到日誌文件,然後再打開它? – usustarr 2011-05-04 21:56:44

相關問題