Jump to content
  • Advertisement
Sign in to follow this  
dmatter

Whats the propper way to read/write wchar_t to iofstreams?

This topic is 4707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

take the following code which creates an error from trying to read a wchar_t:
ifstream ifs("somefile.txt");
wchar_t wct = 5;
ifs >> wct;
What is the correct or usual way to read and write a wchar_t? What about the following (havn't tested it):
ifstream ifs("somefile.txt");
wchar_t wct = 5;
ifs.read((char*)&wct, sizeof(wchar_t));
Its curious why the first snippit doesn't work, perhaps wchar_t isn't portable? Any insights are helpful Thanks

Share this post


Link to post
Share on other sites
Advertisement
wchar_t is portable, it's just the standard library prefers it when you keep the type of streams and strings matched. For example using a wifstream if you're going to read wchar_ts. Though, keep in mind that depending on your compiler, you may want to imbue a codecvt on the stream before doing any reading or writing of wide characters.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
wchar_t is portable, it's just the standard library prefers it when you keep the type of streams and strings matched. For example using a wifstream if you're going to read wchar_ts.

I was half aware of wifstream but isn't it possible to read it from a normal ifstream?
Or can i use a wifstream instead but also read the usual char type?

Quote:
Original post by SiCrane
Though, keep in mind that depending on your compiler, you may want to imbue a codecvt on the stream before doing any reading or writing of wide characters

What would that do exactly (imbue isn't in my vocabulary [sad]).

Would my second snippet of code work for reading a wchar_t?

Edit: what about casting to a 'signed short int'? It doesn't have to be human readable in the actual file.

[Edited by - dmatter on August 30, 2005 5:18:01 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!