vc6 printf重定向输出到文件

来源:互联网 发布:c语言类型标识符 编辑:程序博客网 时间:2024/04/18 23:08

原帖地址:

http://blog.csdn.net/benkaoya/article/details/5935626

测试环境:Microsoft Visual C++ 6.0,Win32 Application或Win32 Console Application均可。
[cpp] view plaincopy
#include "stdafx.h"  
#include <stdio.h>  
int APIENTRY WinMain(HINSTANCE hInstance,  
                     HINSTANCE hPrevInstance,  
                     LPSTR     lpCmdLine,  
                     int       nCmdShow)  
{  
    // TODO: Place code here.  
    printf("Redirected to a file. /n");  
    return 0;  
}  
如上代码(编译生成1.exe),在默认的情况下,printf的信息是打印在屏幕上的。如何才能将信息直接写入文件呢?用dos命令“>filename”和“>>filename”作为1.exe的参数运行即可。如:
1.exe >ab.txt        // 文件覆盖
1.exe >>ab.txt      // 文件追加
如此你就可以在1.exe文件的同一目录下看到ab.txt文件了,文件内容正是printf的信息。
当然你也可以在Microsoft Visual C++ 6.0的setting中直接输入运行参数>ab.txt,来运行exe,如下图: