Archived

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

allgoodnames

standard string objects and win32..

Recommended Posts

allgoodnames    122
I''m tring to make something similar to an old fashioned textadventure and I''ve been building the "engine" in pure dos console mode, but recently I''ve been trying out the win32 api and I would like to try converting what I''ve done so far into a windows program. My problem is that the only functions I could find for text-output was TextOut and DrawText both taking only char* strings. And as for input I didn''t find anything other than rudimentary keypress checks. Is there any way of handling input and output for STL strings? If not, which is the easiest way of converting them? http://envy.nu/anykey

Share this post


Link to post
Share on other sites
Oluseyi    2103
Output: std::string::c_str() returns a const char * which you c an pass to TextOut and other Win32 functions.

Input is more complex. Windows really isn''t designed for console application input (it''s a GUI environment after all), but it is definitely possible. You can even redirect cin and cout to your own console. It''s just not terribly elegant.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Oluseyi    2103
void RedirectIOToConsole(void)
{
int hConHandle;
long lStdHandle;
FILE *fp;

// redirect unbuffered STDOUT to the console
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
}

This function assumes you have already obtained a console, preferrably via a call to AllocConsole().

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Oluseyi    2103
Sorry, the above only redirects cout. To redirect cin and cerr add the following:
// cin:
lStdHandle = (long)GetStdHandle( STD_INPUT_HANDLE );
hConHandle = _open_osfhandle( lStdhandle, _O_TEXT );
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
_setvbuf( stdin, NULL, _IONBF, 0 );
// cerr:
lStdHandle = (long)GetStdHandle( STD_ERROR_HANDLE );
hConHandle = _open_osfhandle( lStdhandle, _O_TEXT );
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
_setvbuf( stderr, NULL, _IONBF, 0 );


[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Kylotan    9858
Also consider whether you can change your design so that, instead of using cin and cout, you can use any istream and ostream objects. This would actually be great for testing purposes: put all your input into a file and read it in with an ifstream... if your program operates on istreams then an ifstream will function the same as cin would, and can write out to any ostream, such as an ofstream (a file) or cout, which you know about.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites