Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > read unicode text file

Reply
Thread Tools

read unicode text file

 
 
markww
Guest
Posts: n/a
 
      07-24-2006
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

 
Reply With Quote
 
 
 
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      07-24-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Convert unicode escape sequences to unicode in a file Jeremy Python 0 01-11-2011 11:39 PM
Convert unicode escape sequences to unicode in a file Jeremy Python 1 01-11-2011 10:36 PM
File.read(fname) vs. File.read(fname,File.size(fname)) Alex Dowad Ruby 4 05-01-2010 08:20 AM
read a ruby script like you would read a text file Mmcolli00 Mom Ruby 2 01-27-2009 10:52 PM
Read Text File and split them to individual text file Krish ASP .Net 1 10-20-2005 03:39 PM



Advertisments