0

Possible Duplicate:
How to open an std::fstream (ofstream or ifstream) with a unicode filename?

I have a string encoded in UTF-16 and I want to create a file, where the name of the file would be this string.

UTF-16LE string looks like:

enter image description here

At first I want to make sure that system sees and displays correctly this name.

I try:

char *output=some address (points to memory where line is held)
ofstream out(output);
out.close();

On output I don't get proper name.
It looks like:
enter image description here

For creating of the highlighted file I appended UTF-16LE mark, not highlighted file was created using just raw UTF-16 line - none of approaches works.

Are there some ways to create files with UTF-16LE names in Windows using only C++ functionality without WinApi (CreateFilew)?

My compiler is MinGW 4.0.4, Windows XP (but I want it to be working on all Windows)

Thanks in advance for any tips!

Community
  • 1
  • 1
Tebe
  • 3,176
  • 8
  • 40
  • 60
  • 1
    I believe Windows uses UTF-8 on filesystem, so you'll probably need to convert the string to UTF-8. – peterph Jan 04 '13 at 22:49
  • they advice to use UTF-16 for everything http://msdn.microsoft.com/en-us/library/windows/desktop/dd374081%28v=vs.85%29.aspx , if I'm not wrong – Tebe Jan 04 '13 at 22:53
  • Carl Norum, I cannot use solution from there, because string is not known in advance, http://i.imgur.com/zTtL8.png . I cannot declare my line as wchar_t line[], size may be any – Tebe Jan 04 '13 at 22:59
  • ...`wstring str = [...]; std::fstream file(str.data());`... – Griwes Jan 04 '13 at 23:12
  • @AlexanderShulz you are right: http://stackoverflow.com/questions/2050973/what-encoding-are-filenames-in-ntfs-stored-as – peterph Jan 04 '13 at 23:31
  • Griwes, plz, could you be more precisely, what does mean wstring str=[...] -? String is held in char *line; String ISN'T known in advance, it's NOT contant. If I try to do as you offered I get http://i.imgur.com/ZHwLR.png , did I get you right? Moreover as I know fstream can't accept wchar_t type. What do I misunderstand? – Tebe Jan 05 '13 at 00:04
  • 3
    http://www.utf8everywhere.org – Pavel Radzivilovsky Jan 05 '13 at 13:13

1 Answers1

0

Thanks you all guys, but it seems that C++ streams are helpless in this case (at least I got such opinion).

So I used WinApi:

 #ifndef WIN32    // for Linux
   ofstream out(output);
   out.close();
 #else  // for Windows
   LPWSTR lp=(LPWSTR )output;
   CreateFileW(lp,GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |  
        FILE_SHARE_WRITE,     NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL );
  #endif

And I got an output file with a correct name:

enter image description here

Thanks again!

Tebe
  • 3,176
  • 8
  • 40
  • 60