Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a filesystem::path, how do you open a file in a cross-platform way?

Tags:

c++

c++17

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?

like image 905
Nicol Bolas Avatar asked Jun 30 '16 13:06

Nicol Bolas


People also ask

How do I convert a filesystem path to string?

To convert a std::filesystem::path to a natively-encoded string (whose type is std::filesystem::path::value_type ), use the string() method. Note the other *string() methods, which enable you to obtain strings of a specific encoding (e.g. u8string() for an UTF-8 string).

What is file system navigation?

Overview. Use the <filesystem> APIs for the following tasks: iterate over files and directories under a specified path. get information about files including the time created, size, extension, and root directory. compose, decompose, and compare paths.


1 Answers

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.

like image 108
3 revs Avatar answered Oct 11 '22 04:10

3 revs