C++ MFC控制檯輸出除錯資訊
阿新 • • 發佈:2020-12-22
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();<br><br>
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 }