views:

423

answers:

2

Hello,

I need to open a file as std::fstream (or actually any other std::ostream) when file name is "Unicode" file name.

Under MSVC I have non-standard extension std::fstream::open(wchar_t const *,...)? What can I do with other compilers like GCC (most important) and probably Borland compiler.

I know that CRTL provides _wfopen but it gives C FILE * interface instead of io-streams, maybe there is a non-standard way to create io-stream from FILE *? Is there any boost::ifstream with MSVC like extension for Windows?

Thanks.

A: 

Convert the Unicode filename to a char* string using something like wcstombs() or WideCharToMultiByte() (which gives you far more control over the codepages involved).

Then use the converted filename to open the file.

Michael Burr
File name includes unicode characters that may not be represented in current locale codepage (i.e. Hebrew characters can't be represented in Latin1 8-bit locale), also Windows does not support UTF-8 code pages. So no, this does not work.
Artyom
+2  A: 

Unfortunately there's no standard way to do that, althougg C++0x (1x?) promises to do that. Until then, you properly assumed that a solution can be found in boost, however, the library you're searching for is Boost.Filesystem.

Boost.Filesystem internally uses wide strings by default for it's universal path system, so there are no unicode problems in this regard.

Kornel Kisielewicz
Do you have a reference for C++0X? I remember discussions but no conclusion and I found nothing in the latest draft.
AProgrammer
The problem is the `boost::filesystem` does not supports wpath under MinGW/GCC because boost configuration defines `BOOST_NO_STD_WSTRING` for some reason (even wide strings work quite well under mingw)
Artyom
I've searched a little more and found this: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-closed.html#454, apparently no solution before library TR2.
AProgrammer
I think it is not good solution to add `wchar_t`. I think that MS CRTL just should support UTF-8 strings instead. Windows is only operating system that uses wide API for core parts.
Artyom