c++utf-16wofstreamcodecvt

C++ Cannot instantiate codecvt for writing unicode in txt file


Hell0,

i need to write a text file (csv file in facts, but nvm) supporting unicode with c++. The source code i have to modify already works great, but only support ANSI. It's working with a wofstream :

std::wofstream x;
CString stringToWrite;

/*
 * getting file
 */

x.open (szFile, std::ios_base::out | std::ios_base::trunc);

/*
 * assigning content to stringToWrite
 */

x.write (stringToWrite.GetString(), stringToWrite.GetLength());

The simple solution i found everywhere is to do :

x.imbue(std::locale(x.getloc(),
    new std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>));
x.open (szFile, std::ios_base::out | std::ios_base::trunc);

But for some reasons, i can't instantiate codecvt, visualStudio say me that the parameters doesn't fit with the declaration. I don't understand why. One of my guess was that my compiler doesn't support it, but now i don't know what to think.

I also tried an alternate solution: declaring the file is encoded in utf-16 by manual writing the first char like this :

x << "\0xFFFE";

It's working (exported file is encoded in utf-16) but it's messing all my writing to the file, because i need to 'convert' my CString in utf-16, but after 2 days trying, i can't get something working.

It would be awesome if i can use the first solution, but i really don't understand why my compiler don't want to do it. I cannot find on the internet anyone with the same problem. If anyone know why it's doing this, or have another alternate solution to write utf-16 txt file, it would be really appreciated <3


Solution

  • Problem solved, i found this : http://www.codeproject.com/Articles/9539/A-UTF-Class-for-Reading-and-Writing-Unicode-Fil

    It's exactly what i was looking for.