iostream & Adding Console I/O to a Win32 GUI App

T

Tony Boloney

Hi all,


I'm trying to use the method described in the article

Adding Console I/O to a Win32 GUI App
http://dslweb.nwnexus.com/~ast/dload/guicon.htm

to output info to a separate console window.

I copy-pasted the code into my project and uncommented the last line

ios::sync_with_stdio();

to point cout, cin, etc to the console as well, but for some reason this
doesn't work. Using

fprintf(stdout, "some text")

works, but

cout << "some text" << endl

does nothing.


Could this have something to do with my configuration? (VS.NET, STLport
4.6 with own iostreams (no wrapper), DirectX9)?


Thanks!

T
 
J

John Harrison

Tony Boloney said:
Hi all,


I'm trying to use the method described in the article

Adding Console I/O to a Win32 GUI App
http://dslweb.nwnexus.com/~ast/dload/guicon.htm

to output info to a separate console window.

I copy-pasted the code into my project and uncommented the last line

ios::sync_with_stdio();

to point cout, cin, etc to the console as well, but for some reason this
doesn't work. Using

fprintf(stdout, "some text")

works, but

cout << "some text" << endl

does nothing.


Could this have something to do with my configuration? (VS.NET, STLport
4.6 with own iostreams (no wrapper), DirectX9)?

The author of that article seems to misunderstand what sync_with_stdio does.
Its purpose is to synchronize output to stdout with cout (for instance) so
that writes to these two stream appear in the right order. It certainly has
nothing to do with redirecting anything. In any case synchronization is on
by default and you would normally call sync_with_stdio to turn it off.

stdout and cout are guaranteed to write to the same place by the C++
standard, but the author of that article has written a lot of non-standard
code which has presumably broken that guarantee. He seems to be redirecting
stdout and then assuming that will simply redirect cout.

It could be that if you switch to another STL (for instance the one that
ships with the MS compiler) that this code will work, but I think the only
way to find out is to try it.

Alternatively you could do the proper thing which is to write you own custom
stream buffer classes which will do console I/O for you. Or find something
like that on the web, I'm sure it's been done many times before.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top