C++ MFC控制台输出调试信息

1、#include <conio.h>

2、在需要开启控制台窗口的地方调用
AllocConsole();//注意检查返回值

3、在需要输出调试的时候调用_cprintf等函数
如_cprintf("i=%d
", i);

4、关闭控制台的时候调用
FreeConsole();

注意:上述方法在输出中文时会出现乱码,如果需要输出中文,请使用下面的方法:
AllocConsole();
freopen( "CONOUT$","w",stdout);
printf("i的值为%d
", i);
FreeConsole();

方法二:
#include <io.h>  
#include <fcntl.h>

void InitConsoleWindow()  
{  
    AllocConsole();  
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);  
    int hCrt = _open_osfhandle((long)handle,_O_TEXT);  
    FILE * hf = _fdopen( hCrt, "w" );  
    *stdout = *hf;  
}
BOOL CHelloMFCDlg::OnInitDialog() 
{  
    CDialog::OnInitDialog();

    InitConsoleWindow();  // add
    printf("str = %s
 ", "Debug output goes to terminal
"); 
    ......  
}
原文地址:https://www.cnblogs.com/mypsq/p/5952900.html