1. 程式人生 > >fprintf與stderr、stdout的使用

fprintf與stderr、stdout的使用

txt style 說明 使用 spa 磁盤文件 stdio.h code fprintf

#include <stdio.h>

void main()
{
fprintf(stderr,"soyo8888!");
fprintf(stdout,"soyo15can‘t open it !\n");
printf("soyocan‘t open it!\n");
}

---------------------------------------------

上面程序編譯成fprint文件,運行顯示如下:
soyo8888Cant open it! soyo15Cant open it! soyoCant open it!

若將輸入重定向到一個temp.txt文件中,運行:.
/fprint >temp.txt 結果如下: soyo8888Cant open it! 查看temp.txt文件內容為: soyo15Cant open it!
soyoCan
t open it! 說明: stdout -- 標準輸出設備 (printf("..")) 同 stdout。 stderr -- 標準錯誤輸出設備 兩者默認向屏幕輸出。 但如果用轉向標準輸出到磁盤文件,則可看出兩者區別。stdout輸出到磁盤文件,stderr在屏幕。 strerr是作為程序運行過程中的錯誤顯示出來的,若想將它重寫向到某文件中,需要運行如下命令: ./fprint 2>temp.txt 這樣運行結果就為: soyo15Can
t open it!
soyoCan
t open it! 查看temp.txt文件的內容是: soyo8888Cant open it!

fprintf與stderr、stdout的使用