You wouldn't imagine something as basic as opening a file using the C++ standard library for a Windows application was tricky ... but it appears to be. By Unicode here I mean UTF-8, but I can convert to UTF-16 or whatever, the point is getting an ofstream instance from a Unicode filename. Before I hack up my own solution, is there a preferred route here ? Especially a cross-platform one ?
views:
2697answers:
3I this is a duplicate question. See if any of the answers there can help.
The current versions of Visual C++ the std::basic_fstream have an open()
method that take a wchar_t* according to http://msdn.microsoft.com/en-us/library/4dx08bh4.aspx.
The C++ standard library is not Unicode-aware. char and wchar_t are not required to be Unicode encodings.
On Windows, wchar_t is UTF-16, but there's no direct support for UTF-8 in the standard library (the char datatype is not unicode on Windows)
On Windows, a constructor for filestreams is provided which takes a const wchar_t*
filename, allowing you to create the stream as:
wchar_t name[] = L"filename.txt";
std::fstream file(name);
However, this overload does not seem to be specified by the standard (it only guarantees the presence of the char* version).
Note that just like char on Windows is not UTF8, on other OS'es wchar_t may not be UTF16. So overall, this isn't likely to be portable. Opening a stream given a wchar_t filename isn't defined according to the standard, and specifying the filename in chars may be difficult because the encoding used by char varies between OS'es.