方法1
需要包含頭文件
#include <conio.h>
#include <iostream>
在你需要開始使用控制檯Console時,如果你想程序開啓就啓動,那麼通常在 初始化對話框OnInitDialog() 或 OnCreate()函數中添加
AllocConsole();//控制檯調試窗口開啓
FILE *stream;
freopen_s(&stream,"CONOUT$", "w", stdout);//開啓中文控制檯輸出支持
然後在 析構函數或OnClose()添加
FreeConsole(); //釋放
這樣,就可以在任意位置使用 cout進行輸出
方法2
需要包含頭文件
#include <conio.h>
在主對話框OnInitDialog()函數中添加
AllocConsole();
在需要顯示數據時(這裏使用的unicode數據集)
HANDLE hdlWrite = GetStdHandle(STD_OUTPUT_HANDLE); //這裏也可以使用STD_ERROR_HANDLE
DWORD dw;
TCHAR ch[256] = _T("你好");
WriteConsole(hdlWrite, ch, lstrlen(ch), &dw, NULL);
WriteConsole(hdlWrite, "\n", strlen("\n"), &dw, NULL);
double testDouble = 2017.0101;
TCHAR ch[256];
memset(ch, 0, 256);
_stprintf_s(ch, _T("%.4f"), testDouble);
WriteConsole(m_hdlWrite, ch, lstrlen(ch) , &m_dw, NULL);
WriteConsole(m_hdlWrite, "\n", strlen("\n"), &m_dw, NULL);
在程序結束時 如 OnClose() 中進行釋放
FreeConsole();