468,512 Members | 1,495 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

read unicode text file

Hi,

I'm just trying to print the contents of a notepad file I saved with
unicode encoding on my win xp machine. I keep getting strange
characters printed out though. Why doesn't this work?

std::wifstream wifile("C:\\unicode.txt");

if (wifile.is_open()) {
wchar_t wszBuffer[MAX_PATH];

while (wifile.getline(wszBuffer, MAX_PATH)) {
AfxMessageBox(wszBuffer);
}
}

My unicode saved file just has the contents "hello" in it, but my
message box prints some crazy stuff.

Thanks

Jul 24 '06 #1
1 3786
markww wrote:
Hi,

I'm just trying to print the contents of a notepad file I saved with
unicode encoding on my win xp machine.
There are at least three ways to do that, and that's just counting
Notepad.
There are more ways to serialize Unicode.
I keep getting strange
characters printed out though. Why doesn't this work?

std::wifstream wifile("C:\\unicode.txt");

if (wifile.is_open()) {
wchar_t wszBuffer[MAX_PATH];

while (wifile.getline(wszBuffer, MAX_PATH)) {
AfxMessageBox(wszBuffer);
}
}
Basically, because wifstream doesn't understand Unicode. What you need
is "a" unicode codecvt. Since there are multiple ways to serialize
Unicode,
which codecvt you need depends the encoding. And unfortunately, the
standard doesn't require one Unicode codecvt. You might need to buy
one.
The easier solution, in many cases: Restrict yourself to UTF-8; read
entire
lines at a time, and convert them yourself.

HTH,
Michiel Salters

Jul 24 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Achim Domma | last post: by
17 posts views Thread by Guyon Morée | last post: by
4 posts views Thread by Kueishiong Tu | last post: by
10 posts views Thread by Tibby | last post: by
2 posts views Thread by starffly | last post: by
reply views Thread by Steve Holden | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.