CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2012
    Posts
    3

    Open unicode filename with C++

    Hello

    I have no idea how to open a file which its name is unicode letters ? usually :

    Code:
    basic_ifstream<wchar_t> src("source.txt");
    Work well to read file with unicode content not filename, so that example doesn't work :
    Code:
    basic_ifstream<wchar_t> src(L"source.txt");
    Also, I have seen some alternatives for using open function but it doesn't work as well.
    Code:
    basic_ifstream<wchar_t> src;
    src.open(L"source.txt");
    I use g++ compiler.
    Regards

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Open unicode filename with C++

    Quote Originally Posted by Emapat View Post
    Hello

    I have no idea how to open a file which its name is unicode letters ?
    You can use OS system calls or something like the boost file system library. The standard library has no say on file naming conventions or character set used for file names.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Open unicode filename with C++

    You could simply convert the name to ansi since (correct me if I'm wrong) unicode filenames are illegal in *nix.

    Also, the L"blah blah" is not a unicode string, it is a wide string. Unicode starts with u and is only available in C++11.

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Open unicode filename with C++

    >> (correct me if I'm wrong) unicode filenames are illegal in *nix.
    In Linux (and other *nix are probably similar), most file systems deal with filenames as a stream of bytes and don't care about the encoding. So the encoding ultimately comes from the app creating the file. If your locale is UTF8, then it is possible to have UTF8 filenames. (If you mount an NTFS/FAT partition, there are mount options on how to handle filename encoding.)

    >> Also, the L"blah blah" is not a unicode string ...
    A more accurate statement would be:
    "The encoding used for wide strings is implementation defined. On Windows, the encoding is UTF16LE. On *nix, the encoding is typically UTF32 in native byte order."

    If using gcc or MSVC, wide strings are Unicode strings (of some encoding). But only a C++11 compiler can give you a guaranteed Unicode encoding of a string literal using u8/u/U.

    In depth description of how string literals are encoded in MSVC and gcc can be found here: http://cboard.cprogramming.com/cplus...ml#post1086757

    gg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured