This topic is now archived and is closed to further replies.


cout w/ Win32 console

Recommended Posts

In this Win32 app, how would I get the standard C++ input/output stream operators (cin & cout) to work with an allocated console? I already know about WriteConsole(), but it would be nice to just use cout. Is this possible?
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

	cout << "hello";	//doesn''t work


	MessageBox(0,"Program Complete", "", 0);

	return 0;
Thanks in advance, -Mike

Share this post

Link to post
Share on other sites
niyaw    158
iostreams don''t seem to provide a way to open a file by anything other than its filename (stdio routines can open os file handles and low-level io file handles), so you can implement your own streambuf class that calls WriteConsole, or you can use something like the following hack:

to reassign stdout to console, and cout will end up printing to console too.

Share this post

Link to post
Share on other sites
niyaw    158
Original post by doctorsixstring
I like the idea of reassigning stdout to the console, but I''m unsure of the exact procedure.

this is the code i use:

*stdout = *_tfdopen(_open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), _O_WRONLY), _T("a"));

you also want to save *stdout so that you can restore it before program terminates to avoid double-closing files or any other possible weird problems.

the main drawback of this method is that it''s highly non-portable. i assume the following things about stdio implementation:
- by including <stdio.h>, i get the definition of FILE struct, not just a declaration. note that this definition isn''t required for using any stdio functions, and you should treat FILE * as an opaque pointer.
- i can copy FILE structs around without any problems (a reasonable assumption considering that FILE is a C struct and doesn''t have ctors/dtors).
- ''stdout'' is a variable, not a macro resolving to a function, such as

#define stdout (crtInternalGetStdout())

this method works on my vc7, but it may not work on dev-c++, vc6 or vc8. the thing most likely to break is FILE definition in stdio.h, because as i said you never use contents of FILE struct in your code.

there''s freopen() function that, unfortunately, only works with files. what we''re looking for is something like _fdreopen that will reopen a file stream given a low-level io handle, but no such thing exists on msvc.
You mentioned the rdbuf() method in one of the other threads, niyaw. Is that what I want to use?

that''s a non-hackish way, but it comes with problems. here''s how you can reassign cout to a file:

// open a file stream

ofstream out("filename");
// save cout''s stream buffer

streambuf *sb = cout.rdbuf();
// point cout''s stream buffer to that of the open file

// now you can print to file by writing to cout

cout << "Hello, world!";
// restore cout''s buffer back


problem with doing the same thing for console is that a console isn''t a file, and basic_filebuf''s constructor only takes a filename. thus, you need to implement your own streambuf class that will call WriteConsole, create an instance of it, and then use rdbuf() to use it with cout. advantage of this method is that it''s portable across win32 compilers. the obvious disadvantage is that you need to write a custom streambuf class for this to work.

so, it''s either a streambuf implementation, a crt extension (you can write _fdreopen given stdio source), or nonportable hacks. take your pick.

Share this post

Link to post
Share on other sites
SteveC    122
There was an article in CUJ about this very thing.

Basically you make a new type of streambuf object which dumps things to an allocated console.

Then you do the assignment of the streambuf trick.

Or just use an accessor to your special ''ostream'' instance.

Check out for the details...

I can''t remember it off the top of my head

Share this post

Link to post
Share on other sites