如何将C语言中的标准输出STDOUT重定向到缓冲区或磁盘文件,而不是屏幕?

解决方案 »

  1.   

    重定向到文件#include <stdio.h>
    #include <stdlib.h>FILE *stream;void main( void )
    {
       /* Reassign "stdout" to "yourfile.name": */
    char * pch = new char[100];
       stream = freopen( "yourfile.name", "w", stdout );   if( stream != NULL )
       {   
          fprintf( stream, "This will go to the file 'yourfile.name'\n" );
          fclose( stream );
       }
    }
      

  2.   

    事实上就是这个WIN32 函数SetStdHandle
      

  3.   

    sorry这一行
     
    fprintf( stream, "This will go to the file 'yourfile.name'\n" );
    应该是printf("This is will go to the file 'yourfile.name'\n");