关于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必须时在运行时刻。

posted @ 2012-10-13 11:11  cseriscser  阅读(6519)  评论(0编辑  收藏  举报