2011-08-31 50 views
2

我一直在試圖讓一個程序的標準輸出重定向到一個文件。到目前爲止,這個代碼效果很好:將標準輸出重定向到沒有ANSI警告的文件

FILE *output = fopen("output","w"); 
if (dup2(fileno(output),1) == -1) 
{ 
    /* An error occured. */ 
    exit(EXIT_FAILURE); 
} 

問題是,我試圖堅持ANSI C,fileno不是ANSI。當我用gcc編譯我得到的警告:

gcc -Wall -ansi -pedantic 
warning: implicit declaration of function ‘fileno’ 

有沒有辦法在所有標準輸出重定向到ANSI C文件?

回答

2

ANSI C的方式做到這一點是freopen()

if (freopen("output", "w", stdin) == NULL) { 
    /* error occured */ 
    perror("freopen"); 
} 
+0

謝謝,這確實起作用。 –

相關問題