title: Using a filesystem::path, how do you open a file in a cross-platform way?
body: <p>Let's say you have used the new <code>std::filesystem</code> (or <code>std::experimental::filesystem</code>) code to hunt down a file. You have a <code>path</code> variable that contains the full pathname to this variable.</p>
  
  <p>How do you open that file?</p>
  
  <p>That may sound silly, but consider the obvious answer:</p>
  
  <pre><code>std::filesystem::path my_path = ...;
  std::ifstream stream(my_path.c_str(), std::ios::binary);
  </code></pre>
  
  <p>This is not <em>guaranteed</em> to work. Why? Because on Windows for example, <code>path::string_type</code> is <code>std::wstring</code>. So <code>path::c_str</code> will return a <code>const wchar_t*</code>. And <code>std::ifstream</code> can <em>only</em> take paths with a <code>const char*</code> type.</p>
  
  <p>Now it turns out that this code will actually function in VS. Why? Because Visual Studio has a <a href="https://msdn.microsoft.com/en-us/library/zek0beca.aspx" rel="noreferrer">library extension that does permit this to work</a>. But that's non-standard behavior and therefore not <em>portable</em>. For example, I have no idea if GCC on Windows provides the same feature.</p>
  
  <p>You could try this:</p>
  
  <pre><code>std::filesystem::path my_path = ...;
  std::ifstream stream(my_path.string().c_str(), std::ios::binary);
  </code></pre>
  
  <p>Only Windows confounds us again. Because if <code>my_path</code> 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.</p>
  
  <p>Boost Filesystem actually had a similar problem. But they extended their version of iostreams to support <code>path</code>s directly.</p>
  
  <p>Am I missing something here? Did the committee add a cross-platform filesystem library without adding a cross-platform way to <em>open</em> files in it?</p>