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. Opening a file with name in UNICODE format

Opening a file with name in UNICODE format

Scheduled Pinned Locked Moved C / C++ / MFC
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.
  • A Offline
    A Offline
    Ajay L D
    wrote on last edited by
    #1

    Hi, I am trying to open a file, whose name is in UNICODE string with a Byte-order Mark (BOM). How do I open such files? The CFile class fails to recognize such a name. Thank you. AJ

    M 1 Reply Last reply
    0
    • A Ajay L D

      Hi, I am trying to open a file, whose name is in UNICODE string with a Byte-order Mark (BOM). How do I open such files? The CFile class fails to recognize such a name. Thank you. AJ

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      #include <fstream>
      wchar_t file_name[] = L"hello.txt"; // ... Your file name source.
      // std::fstream f(file_name, std::ios::in | std::ios::out);
      std::wfstream f(file_name, std::ios::in | std::ios::out);


      Maxwell Chen

      modified on Thursday, December 27, 2007 4:01:06 AM

      A 1 Reply Last reply
      0
      • M Maxwell Chen

        #include <fstream>
        wchar_t file_name[] = L"hello.txt"; // ... Your file name source.
        // std::fstream f(file_name, std::ios::in | std::ios::out);
        std::wfstream f(file_name, std::ios::in | std::ios::out);


        Maxwell Chen

        modified on Thursday, December 27, 2007 4:01:06 AM

        A Offline
        A Offline
        Ajay L D
        wrote on last edited by
        #3

        Thank you for your reply. But an existing file with Byte-order Mark (Ex: FFFE), will not open with the above code.

        M 1 Reply Last reply
        0
        • A Ajay L D

          Thank you for your reply. But an existing file with Byte-order Mark (Ex: FFFE), will not open with the above code.

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          Ajay L D wrote:

          But an existing file with Byte-order Mark (Ex: FFFE), will not open with the above code.

          I just made a file with the mark EF BB BF in the beginning three bytes of the content, and the content is in Chinese. I use the below code, and it can open and load data into fstream variable. (Sorry for my typo in last reply. Note the "w" of the type name.)

          std::wifstream file(file_name, std::ios::in | std::ios::binary);

          12-27 05:12 pm> The content loaded is incorrect... :sigh: 12-27 06:10 pm> Oh you have to use binary mode to open Unicode files. Reference: www.gamedev.net/community/forums/topic.asp?topic_id=362755


          Maxwell Chen

          modified on Thursday, December 27, 2007 5:11:48 AM

          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