Original Source: http://www.halcyon.com /~ AST/dload/guicon.htm.
I modified it and merged it into a head file named guicon. h. After debugging in vs2005, some code has been modified to adapt to 64-bit compilation. The usage is not much said. You only need to call redirectiotoconsole () once before use. Very simple.
# Ifndef _ guicon_h __
# DEFINE _ guicon_h __
# Ifdef _ debug
# Include <windows. h>
# Include <stdio. h>
# Include <fcntl. h>
# Include <Io. h>
# Include <iostream>
# Include <fstream>
# Ifndef _ use_old_iostreams
Using namespace STD;
# Endif
// Maximum mumber of lines the output console shocould have
Static const word max_console_lines = 500;
Void redirectiotoconsole ();
Void redirectiotoconsole ()
...{
Int hconhandle;
Handle lstdhandle;
Lele_screen_buffer_info coninfo;
File * FP;
// Allocate a console for this app
Allocconsole ();
// Set the screen buffer to be big enough to let us scroll text
Getconsolescreenbufferinfo (getstdhandle (std_output_handle ),
& Coninfo );
Coninfo. dwsize. Y = max_console_lines;
Setconsolescreenbuffersize (getstdhandle (std_output_handle ),
Coninfo. dwsize );
// Redirect unbuffered stdout to the console
Lstdhandle = getstdhandle (std_output_handle );
Hconhandle = _ open_osfhandle (int_ptr) lstdhandle, _ o_text );
Fp = _ fdopen (hconhandle, "W ");
* Stdout = * FP;
Setvbuf (stdout, null, _ ionbf, 0 );
// Redirect unbuffered stdin to the console
Lstdhandle = getstdhandle (std_input_handle );
Hconhandle = _ open_osfhandle (int_ptr) lstdhandle, _ o_text );
Fp = _ fdopen (hconhandle, "R ");
* Stdin = * FP;
Setvbuf (stdin, null, _ ionbf, 0 );
// Redirect unbuffered stderr to the console
Lstdhandle = getstdhandle (std_error_handle );
Hconhandle = _ open_osfhandle (int_ptr) lstdhandle, _ o_text );
Fp = _ fdopen (hconhandle, "W ");
* Stderr = * FP;
Setvbuf (stderr, null, _ ionbf, 0 );
// Make cout, wcout, Cin, wcin, wcerr, cerr, wclog and clog
// Point to console as well
IOS: sync_with_stdio ();
}
// End of File
# Endif
# Endif