read unicode text file

M

markww

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
 
M

Michiel.Salters

markww said:
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
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top