read unicode text file

Discussion in 'C++' started by markww, Jul 24, 2006.

  1. markww

    markww Guest

    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
     
    markww, Jul 24, 2006
    #1
    1. Advertising

  2. markww

    Guest

    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, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krish
    Replies:
    1
    Views:
    1,084
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 20, 2005
  2. Jeremy
    Replies:
    1
    Views:
    810
    Alex Willmer
    Jan 11, 2011
  3. Jeremy
    Replies:
    0
    Views:
    580
    Jeremy
    Jan 11, 2011
  4. Mmcolli00 Mom
    Replies:
    2
    Views:
    197
    Mmcolli00 Mom
    Jan 27, 2009
  5. Alex Dowad
    Replies:
    4
    Views:
    273
    Michel Demazure
    May 1, 2010
Loading...

Share This Page