Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. UNICODE problem: fgetws

UNICODE problem: fgetws

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IGx89
    wrote on last edited by
    #1

    I'm trying to read a file (which could be using ANSI or MBCS/UNICODE encoding) using the fgetws function, but am having problems. Here's my code: FILE *file = fopen(tmp,"r"); wchar_t *line = new wchar_t[2001]; wcscpy(line,L""); wchar_t buf[501]; while(fgetws(buf,500,file) != NULL) wcscat(line,buf); The problem is that only the first fgetws function call seems to work correctly; all the following calls return semi-invalid strings, almost double-UNICODE encoded: if there are, say, three spaces, they are stored as 00 00 00 32 00 00 00 32 00 00 00 32 in memory! I'm completely new to UNICODE programming, so I certainly could be making a simple mistake; I just haven't been able to find it yet :(.

    M 1 Reply Last reply
    0
    • I IGx89

      I'm trying to read a file (which could be using ANSI or MBCS/UNICODE encoding) using the fgetws function, but am having problems. Here's my code: FILE *file = fopen(tmp,"r"); wchar_t *line = new wchar_t[2001]; wcscpy(line,L""); wchar_t buf[501]; while(fgetws(buf,500,file) != NULL) wcscat(line,buf); The problem is that only the first fgetws function call seems to work correctly; all the following calls return semi-invalid strings, almost double-UNICODE encoded: if there are, say, three spaces, they are stored as 00 00 00 32 00 00 00 32 00 00 00 32 in memory! I'm completely new to UNICODE programming, so I certainly could be making a simple mistake; I just haven't been able to find it yet :(.

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      If opened in Text mode (the default), the MS C run-time treats the file as if it is ANSI (i.e. encoded using your default locale's character set). fgetws passes the data read through MultiByteToWideChar to get a UTF-16 string. If the file is already UTF-16, you'll get the wrong answer (what you're seeing here). Files opened in Binary mode (by adding a 'b' to the mode parameter) are treated as-is, with no conversions. This also means that CR+LF pairs are not converted to line feeds alone: you'll see \r as well as \n. Stability. What an interesting concept. -- Chris Maunder

      I 1 Reply Last reply
      0
      • M Mike Dimmick

        If opened in Text mode (the default), the MS C run-time treats the file as if it is ANSI (i.e. encoded using your default locale's character set). fgetws passes the data read through MultiByteToWideChar to get a UTF-16 string. If the file is already UTF-16, you'll get the wrong answer (what you're seeing here). Files opened in Binary mode (by adding a 'b' to the mode parameter) are treated as-is, with no conversions. This also means that CR+LF pairs are not converted to line feeds alone: you'll see \r as well as \n. Stability. What an interesting concept. -- Chris Maunder

        I Offline
        I Offline
        IGx89
        wrote on last edited by
        #3

        Ok, thanks for the help! So I'll just try reading the file as binary, and see how that works. Is using fgetws the best way to fill a char array with the contents of a file, in a straight-Win32 app?

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups