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. Problem reading a unicode text file

Problem reading a unicode text file

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
4 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.
  • B Offline
    B Offline
    balu chettri
    wrote on last edited by
    #1

    //I have a problem reading a text file which I have //created using Notepad and saved at c:\test.txt in //"Unicode" format. //The file contains just a single word: "Test" //The following is a C++ program to read the file #include #include using namespace std; typedef basic_ifstream ifs; void main () { ifs is ("c:\\test.txt"); wint_t c; while ((c = is.get()) != WEOF) { wprintf (L"%c", c); } } //The output was: //..T.e.s.t. //Note that the loop went for 10 times while I expected it //to run only 4 times for word "Test". //The following is an equivalent C program that does all correctly. /* #include void main () { FILE *fp = _wfopen (L"c:\\test.txt", L"rb"); wint_t c; while ((c = fgetwc(fp)) != WEOF) { wprintf (L"%c", c); } fclose (fp); } */ //Output: Test //Do you have any suggestions? Please help. Thanks in advance! B2C

    D 1 Reply Last reply
    0
    • B balu chettri

      //I have a problem reading a text file which I have //created using Notepad and saved at c:\test.txt in //"Unicode" format. //The file contains just a single word: "Test" //The following is a C++ program to read the file #include #include using namespace std; typedef basic_ifstream ifs; void main () { ifs is ("c:\\test.txt"); wint_t c; while ((c = is.get()) != WEOF) { wprintf (L"%c", c); } } //The output was: //..T.e.s.t. //Note that the loop went for 10 times while I expected it //to run only 4 times for word "Test". //The following is an equivalent C program that does all correctly. /* #include void main () { FILE *fp = _wfopen (L"c:\\test.txt", L"rb"); wint_t c; while ((c = fgetwc(fp)) != WEOF) { wprintf (L"%c", c); } fclose (fp); } */ //Output: Test //Do you have any suggestions? Please help. Thanks in advance! B2C

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      balu_codeproject wrote:

      typedef basic_ifstream ifs;

      Try:

      typedef wifstream ifs;


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      B 2 Replies Last reply
      0
      • D David Crow

        balu_codeproject wrote:

        typedef basic_ifstream ifs;

        Try:

        typedef wifstream ifs;


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        B Offline
        B Offline
        balu chettri
        wrote on last edited by
        #3

        Thanks for your help! I hope the problem is solved.

        1 Reply Last reply
        0
        • D David Crow

          balu_codeproject wrote:

          typedef basic_ifstream ifs;

          Try:

          typedef wifstream ifs;


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          B Offline
          B Offline
          balu chettri
          wrote on last edited by
          #4

          Unicode Text File c:\test.txt contents: "Test" (without quotes). wchar_t is defined as unsigned short as it should be. typedef unsigned short wchar_t; But wifstream which is defined as typedef basic_ifstream > wifstream; cannot read unicode file correctly. #include using namespace std; void main () { wifstream is ("c:\\test.txt"); wchar_t c; while ((c = is.get ()) != WEOF) { wprintf (L"%c", c); } } output was: ..T.e.s.t. When I tried short instead of wchar_t all went ok; #include using namespace std; void main () { typedef basic_ifstream > ifs; ifs is ("c:\\test.txt"); wchar_t c; while ((c = is.get ()) != WEOF) { wprintf (L"%c", c); } } output was: Test Should wchar_t be defined as short or this is a bug in standard C++ library shipped with VC++ 6.0? B2C

          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