Stdout in MFC. You can use cout!

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.