VC程序重定向printf到console窗口

来源:互联网 发布:移动网络电影网站 编辑:程序博客网 时间:2024/06/10 01:33
本方法简单易用,适用于VC6、VC.NET2003
假定你创建的VC应用程序叫做ImageFilter,那么只需添加如下代码即可:


[1] 打开ImageFilter.cpp文件,增加

#include <io.h>
#include <fcntl.h>

 

[2] 在该文件的'CImageFilterApp theApp;'后面增加一个函数:
void InitConsoleWindow(void)
{
   int hCrt;
   FILE *hf;

   AllocConsole();
   hCrt = _open_osfhandle(
        (long)GetStdHandle(STD_OUTPUT_HANDLE),
        _O_TEXT );
   hf = _fdopen( hCrt, "w" );
   *stdout = *hf;
   setvbuf( stdout, NULL, _IONBF, 0 );

 

   // test code ...
   printf("InitConsoleWindow OK!\n\n");
}

 

[3] 在函数'BOOL CImageFilterApp::InitInstance()'中的所有代码前面调用该函数:

   InitConsoleWindow();

 

好了大功告成! 后面你就可以使用printf输出运行信息了 :)

0 0
原创粉丝点击