title: Are ios::app mode and fstream::write somehow incompatible with each other?
body: <p>I am quite puzzled by failure of <code>fstream::write</code> when a file is opened using <code>ios::app</code> mode when there are no such problems when the file is opened using <code>ios::out</code> mode.</p>
  
  <p>Are <code>ios::app</code> mode and <code>fstream::write</code> somehow incompatible with each other?</p>
  
  <p>Sample code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  void test1()
  {
     char const* fname = "out-1.txt";
     std::fstream out;
     out.open(fname, std::ios::app | std::ios::binary);
     if(!out)
     {
        std::cerr &lt;&lt; "Cant open file " &lt;&lt; fname &lt;&lt; " to write to.\n";
        return;
     }
  
     int val = 10;
     out.write(reinterpret_cast&lt;char*&gt;(&amp;val), sizeof(val));
     if (!out )
     {
        std::cerr &lt;&lt; "Error in writing " &lt;&lt; val &lt;&lt; " to file with ios::app mode.\n";
     }
  }
  
  void test2()
  {
     char const* fname = "out-2.txt";
     std::fstream out;
     out.open(fname, std::ios::out | std::ios::binary);
     if(!out)
     {
        std::cerr &lt;&lt; "Cant open file " &lt;&lt; fname &lt;&lt; " to write to.\n";
        return;
     }
  
     int val = 10;
     out.write(reinterpret_cast&lt;char*&gt;(&amp;val), sizeof(val));
     if (!out )
     {
        std::cerr &lt;&lt; "Error in writing " &lt;&lt; val &lt;&lt; " to file with ios::out mode.\n";
     }
  }
  
  int main()
  {
     test1();
     test2();
     return 0;
  }
  </code></pre>
  
  <p>Output of running the program using g++ 4.8.2:</p>
  
  <pre>
  Error in writing 10 to file with ios::app mode.
  </pre>