1 1、#include <conio.h>
2
3 2、在需要开启控制台窗口的地方调用
4 AllocConsole();//注意检查返回值
5
6 3、在需要输出调试的时候调用_cprintf等函数
7 如_cprintf("i=%d\n", i);
8
9 4、关闭控制台的时候调用
10 FreeConsole();
11
12 注意:上述方法在输出中文时会出现乱码,如果需要输出中文,请使用下面的方法:
13 AllocConsole();
14 freopen( "CONOUT$","w",stdout);
15 printf("i的值为%d\n", i);
16 FreeConsole();
1 方法二:
2 #include <io.h>
3 #include <fcntl.h>
4
5 void InitConsoleWindow()
6 {
7 AllocConsole();
8 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
9 int hCrt = _open_osfhandle((long)handle,_O_TEXT);
10 FILE * hf = _fdopen( hCrt, "w" );
11 *stdout = *hf;
12 }
13 BOOL CHelloMFCDlg::OnInitDialog()
14 {
15 CDialog::OnInitDialog();
16
17 InitConsoleWindow(); // add
18 printf("str = %s\n ", "Debug output goes to terminal\n");
19 ......
20 }