# load unicode text from file and rendering in SDL

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

## Recommended Posts

I am trying to load some unicode text from a text file and render it in SDL. Here's my code:

 SDL_Color c = {0x00, 0xff, 0x00}; std::wifstream in(L"data/string.txt"); std::wstring str; in >> str; SDL_Surface *msg = TTF_RenderUNICODE_Blended(font, (Uint16*)str.c_str(), c); 

font is defined as a TTF_Font object.

The text file contains the string "?????". However, it's not displayed, but some other characters (like garbage).
I tried changing the encoding of the file to other encodings like ANSI, utf-8, utf-8 without BOM, UCS2 BE, UCS2 LE but none displays the desired result. The characters change when I change the encodings but they remain garbage.

I changed the encodings using notepad++.

What is the problem? I thought UCS2 BE or LE would work because I am using an std::wstring on Windows, but it doesn't. What's wrong?

##### Share on other sites
You just stumbled into one of the major quirks of the standard library.

Rather than try to explain it myself, allow me to point you to a good explanation somebody else gave me when I stumbled into this same scenario.

It might be smarter to use UTF8 encoding for your files, and then read it in with a regular ifstream, then convert the UTF8 string to wstring with MultiByteToWideChar.

##### Share on other sites
If you decide to use UTF-8 for your files you can just use TTF_RenderUTF8_Blended and no conversion is needed.

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013119
×