關於C/C++中的文件的重定向freopen()


stdin,stdout,stderr

*標准輸入輸出流
#include <stdio.h>

或者(在CPP中)#include <cstdio>

extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;


1.stdin
*0;     //標准文件描述符;
*標准輸入流;默認從鍵盤輸入,可以對其進行重定向。從磁盤文件輸入。

2.stdout
*1;
*標准輸出流;默認向屏幕輸出,可以對其進行重定向。向磁盤文件輸出。

3.stderr
*2;
*標准錯誤輸出流,默認向屏幕輸出,可以對其進行重定向。向磁盤文件輸出。

stderr與stdin,stdout一樣,是流。stderr一般指錯誤流。

 所以在輸出時,可以對stdout進行重定向,把結果輸出到磁盤文件中。而對stderr不進行重定向,對一些錯誤的消息,登錄日志等輸出到屏幕。或者對stderr進行重定向到日志文件,進行相應的輸出。

 1 freopen("c:\\in.txt","r",stdin);
 2 scanf("");//輸入將從c:\\in.txt輸入
 3 //或者
 4 fscanf(stdin,"");//輸入將從c:\\in.txt輸入
 5 
 6 
 7 fropen("c:\\out.txt","w",stdout);
 8 printf("the stream will go into out.txt");//輸出流將向c:\\out.txt輸出。
 9 //或者
10 fprintf(stdout,"");//輸出流將向c:\\out.txt輸出。
11 
12 
13 
14 fropen("c:\\errout.txt","w",stderr);
15 fprintf(stderr,"the error stream will go into errout.txt");// 錯誤信息將向重c:\\errout.txt輸出。
16 //或者
17 fprintf(stderr,"");// 錯誤信息將向重c:\\errout.txt輸出。

另外:

stderr,可以在編譯階段輸出。 

stdout必須時在運行時刻。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM