输出输出重定向简单的说就是改变默认的输入输出方向。即让我们输入输出重定向到我们想要的地方去。
下面是来自MSDN的一个程序:
- #include <stdio.h>
- #include <stdlib.h>
- FILE *stream;
- void main( void )
- {
- /* Reassign "stderr" to "freopen.out": */
- stream = freopen( "freopen.out", "w", stderr );
- if( stream == NULL )
- fprintf( stdout, "error on freopen/n" ); //输出到屏幕
- else
- {
- fprintf( stream, "This will go to the file 'freopen.out'/n" ); //输出到文件
- fprintf( stdout, "successfully reassigned/n" ); //输出到屏幕
- fclose( stream );
- }
- system( "type freopen.out" ); //从文件中读出输出到屏幕
- }