博客
关于我
MFC学习——MFC工程在调试中查看输出信息
阅读量:543 次
发布时间:2019-03-09

本文共 1127 字,大约阅读时间需要 3 分钟。

简介:

MFC工程,一般情况下,不支持常规的cout、printf 等输出函数,即不能通过此类方法在控制台查看输出信息。

但为了方便调试和获取重要信息,不得不利用某些方法来实现数据的输出和显示,所以汇总如下方法,供大家使用

功能:

  在调试MFC程序时,查看特定位置变量的输出值;或者在某特定条件执行时,给出一个输出标识。

方法汇总

方法一:调用TRACE(LPCTSTR lpszFormat, ...)函数

在MFC中使用TRACE函数来打印输出结果却是非常方便,和在控制台程序中使用cout、printf函数的使用方法和效果类似。不过有几点  

    需要注意:

1、TRACE函数的输出是在Output窗口的Debug选项下; 

2、只有在DEBUG版本调试时才会有输出,如果是在

        3、 Release版本调试或者运行程序时,将不会看到输出。

方法二:使用AfxMessageBox( )函数来输出信息

AfxMessageBox()函数在调试时也比较常用,使用方法简单,此处就不做介绍。

方法三:使用fprinf()函数来输出信息

fprinf()函数,可以将需要输出的信息存放在自定义的文件中。

用法:

FILE *gFp = fopen("gFp.txt","w+");

fprintf("Hello , the world !");

但该函数使用,缺点比较明显:程序必须可以正常运行,即程序运行完之后,才可以生成并查看输出信息

方法四:将标准输出定向到自己创建的控制台

      #include "io.h"

      #include "fcntl.h"

        void InitConsole()

      {
            int nRet= 0;
           FILE* fp;
           AllocConsole();
           nRet= _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
           fp = _fdopen(nRet, "w");
          *stdout = *fp;
          setvbuf(stdout, NULL, _IONBF, 0);
    }

  将此函数在MFC程序初始化的地方调用,即可使用控制台查看printf函数的打印信息

附录:

1、VC调试的方法汇总http://blog.csdn.net/chocolateconanlan/article/details/4061545

2、参考链接一:http://blog.csdn.net/brucezong/article/details/6889974

3、参考链接二:http://blog.csdn.net/liuxizhen2009/article/details/8557888

你可能感兴趣的文章
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>