1

Possible Duplicate:
Opening fstream with file with Unicode file name under Windows using non-MSVC compiler

I have been trying to find a simple way to open a file with non-ASCII characters in its filename in Windows with GCC. Microsoft's STL provides wstring overloads for the fstream classes, but Standard C++ does not, because `wchar_t´ isn't well-defined.

I'm looking for a way to open a fstream when given a std::wstring for a filename, in Standard C++ and Win32 API.

I do not want to create my own streambuf subclass that takes a std::wstring and imitates fstream. I do need to imitate the fstream::open(const std::wstring &) (or whatever it's declared as) function, so I can open weirdly named files without a hitch.

I have already thought of using the short DOS 8.3 filenames, but the GetShortPathName takes and outputs wide strings, unusable for the STL constructor/open function. I do have methods in place to convert to different codepages, so perhaps converting the wide string to local codepage will give the correct 8-bit string that allows me to open an fstream?

Community
  • 1
  • 1
rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • @raj: oh, yes, I forgot to mention, Boost, as good as it may be, is a bit too intrusive here for me to use it. I need a `std::fstream+std::wstring` combined with the Win32 API. The MSVC STL does it, I just don't know how :( – rubenvb Jun 25 '11 at 16:41
  • Have you tried using WideCharToMultiByte and passing the result to gcc fstream::open ? Only works if the wstring can be expressed correctly in the current local code page – parapura rajkumar Jun 25 '11 at 16:43
  • @raj: if the current local codepage cannot represent the filename, what would the filename look like in Explorer? Or if printed by the `dir` command? How would you tell `delete` to remove the file? Wouldn't you always need to do that through some local codepage? I find this frustrating, that it is seemingly possible, but no one seems to know how. – rubenvb Jun 25 '11 at 17:13
  • Internally all Win32API that takes strings have two versions. Example OpenFileA and OpenFileW. OpenFileW takes the wchar_t version that fstream::open(const std::wstring &) uses. – parapura rajkumar Jun 25 '11 at 17:22
  • Well, using my default local codepage 850, Saving a file with a name containing the Euro symbol €, produces question marks instead of that symbol. When I try to enter a € symbol, I get a question mark. When I try to open an `fstream` with a question mark instead of the Euro symbol, it fails. There seems to be absolutely no way to work this out... – rubenvb Jun 25 '11 at 17:23
  • @Raj: What would I use to link `OpenFileW` to `std::ifstream`? – rubenvb Jun 25 '11 at 17:25
  • The MSVC STL ships with source for both the STL and the CRT. So why not just step through what it does to understand how to do this? – Martyn Lovell Jun 25 '11 at 21:01
  • "but Standard C++ does not, because `wchar_t` isn't well-defined." While it's true that several properties of `wchar_t` are implementation defined, I find it far more likely that this is simply a historical oversight like how iostreams have no knowledge of `std::string`. – Lightness Races in Orbit Jun 26 '11 at 16:11
  • @Tomalak Geret'kal: 99% right. Not an oversight, time pressure. It's quite likely that a well-written proposal would have made it. – MSalters Jun 27 '11 at 08:30

0 Answers0