title: Why does std::fopen and std::filebuf::open trim trailing space characters on Windows?
body: <p>I stumbled on a strange problem when trying to port a piece of C++ code from MSWindows to linux (Mint10). I discovered that there was a little typo in a hard-coded filename : a trailing space char (\x20). </p>
  
  <p>The code worked like a charm under Windows (Windows Vista/7), but it had trouble to deal with the erroneous filename under linux. I discovered that both <code>std::filebuf::open</code> and <code>std::fopen</code> (and also <code>boost::filesystem::filebuf</code>) allow trailing space chars in filename under Windows (which I think is an incorrect behavior).</p>
  
  <p>I tested the following code using MSVC2010 and MinGW under Win7 :</p>
  
  <pre><code># include &lt;iostream&gt;
  # include &lt;fstream&gt;
  # include &lt;string&gt;
  # include &lt;cstdlib&gt;
  
  const std::string filename = "Z:\\Path\\To\\A\\File " ;  // one trailing space
  
  int main(int, char * [])
  {
      std::cout &lt;&lt; "Testing with file : \"" &lt;&lt; filename &lt;&lt; "\"" &lt;&lt; std::endl ;
  
      // Testing with std::filebuf::open
      std::filebuf fb ;
      fb.open(filename.c_str(), std::ios::in | std::ios::binary) ;
      if( fb.is_open() )
      {
          std::cout &lt;&lt; "Using std::filebuf : Opened" &lt;&lt; std::endl ;
          fb.close() ;
      }
      else
      {
          std::cout &lt;&lt; "Using std::filebuf : Not opened" &lt;&lt; std::endl ;
      }
  
      // Testing with std::fopen
      std::FILE * fp = std::fopen(filename.c_str(), "rb") ;
      if( fp )
      {
          std::cout &lt;&lt; "Using std::fopen : Opened" &lt;&lt; std::endl ;
          std::fclose(fp) ;
      }
      else
      {
          std::cout &lt;&lt; "Using std::fopen : Not opened" &lt;&lt; std::endl ;
      }
      return 0 ;
  }
  </code></pre>
  
  <p>I verified that the file on the harddrive has no trailing space characters in the name. The file was successfully opened on Windows, but no luck to access it under linux if the filename isn't an exact match.</p>
  
  <p>So I have a couple of questions :</p>
  
  <ul>
  <li>Is this a normal behavior of the file API under Windows ? And is there some other gotchas ? I googled but found no reference on this case. (Maybe I'm a bad googler :))</li>
  <li>How to write a code that works in a strict fashion with filenames ? Always trimming filenames before passing them to some file function isn't the solution for my case ; my current app have to list directories (using <code>boost::filesystem::directory_iterator</code>) and trimming filenames will break the app if the user provides filenames with trailing space chars (which is rare but allowed on many file systems).</li>
  </ul>
  
  <p>Thanks for any advice !</p>