34

Let's say you have used the new std::filesystem (or std::experimental::filesystem) code to hunt down a file. You have a path variable that contains the full pathname to this variable.

How do you open that file?

That may sound silly, but consider the obvious answer:

std::filesystem::path my_path = ...;
std::ifstream stream(my_path.c_str(), std::ios::binary);

This is not guaranteed to work. Why? Because on Windows for example, path::string_type is std::wstring. So path::c_str will return a const wchar_t*. And std::ifstream can only take paths with a const char* type.

Now it turns out that this code will actually function in VS. Why? Because Visual Studio has a library extension that does permit this to work. But that's non-standard behavior and therefore not portable. For example, I have no idea if GCC on Windows provides the same feature.

You could try this:

std::filesystem::path my_path = ...;
std::ifstream stream(my_path.string().c_str(), std::ios::binary);

Only Windows confounds us again. Because if my_path happened to contain Unicode characters, then now you're reliant on setting the Windows ANSI locale stuff correctly. And even that won't necessarily save you if the path happens to have characters from multiple languages that cannot exist in the same ANSI locale.

Boost Filesystem actually had a similar problem. But they extended their version of iostreams to support paths directly.

Am I missing something here? Did the committee add a cross-platform filesystem library without adding a cross-platform way to open files in it?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    The extension is very likely to become standardized [Defect Report 2676](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#2676). Might have happened last week. – Bo Persson Jun 30 '16 at 13:40
  • Would `basic_ifstream stream(my_path);` work, or am I missing something there? It's certainly not pretty... – chris Jun 30 '16 at 13:45
  • @chris: No. The `basic_ifstream`'s character type refers to the character type of the *data* it reads/writes, not of the path it takes. – Nicol Bolas Jun 30 '16 at 13:46
  • @NicolBolas, Right, my bad. – chris Jun 30 '16 at 13:46

1 Answers1

16

Bo Persson pointed out that this is the subject of a standard library defect report. This defect has been resolved, and C++17 will ship, requiring implementations where path::value_type is not char to have their file stream types take const filesystem path::value_type*s in addition to the usual const char* versions.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982