Author blog: http://87year.info
The majority of c/c ++ learning is started from the console, and it is very convenient to directly output log information in the console.
Today, I have Baidu a variety of methods for outputting logs to the console in the mfc program. Finally, I have compared a convenient and simple method that can be turned off at any time.
/* Required header files
# Include <stdio. h>
# Include <io. h>
# Include <fcntl. h>
*/
Int hCrt = 0;
// Create a console <! -- More -->
Void OnBnClickedButton1 ()
{
FreeConsole ();
If (AllocConsole ())
{
HCrt = _ open_osfhandle (long) GetStdHandle (STD_OUTPUT_HANDLE), _ O_TEXT );
* Stdout = * (: _ fdopen (hCrt, "w "));
: Setvbuf (stdout, NULL, _ IONBF, 0 );
* Stderr = * (: _ fdopen (hCrt, "w "));
: Setvbuf (stderr, NULL, _ IONBF, 0 );
}
}
// Release the Console
Void OnBnClickedButton2 ()
{
FreeConsole ();
}
Output Information
Void OnEnUpdateEdit1 ()
{
Printf ("hello MFConsole ");
}
Before exiting, FreeConsole () is required. Otherwise, an error will occur.