黑暗血时代最新章节:printf重定向输出到文件

来源:百度文库 编辑:九乡新闻网 时间:2024/05/03 11:38:42
printf重定向输出到文件
2010-10-12 14:17 460人阅读 评论(0) 收藏 举报
测试环境:Microsoft Visual C++ 6.0,Win32 Application或Win32 Console Application均可。
view plain
#include "stdafx.h"
#include 
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,如下图: