title: Opening a binary output file stream without truncation
body: <p>I am trying to open a binary output file to which I need to append some data. I cannot output the data sequentially, so I need to be able to seek within the file stream and cannot use the <code>std::ios::app</code> flag.</p>
  
  <p>Unfortunately, when opening an output file stream without the <code>std::ios::app</code> flag, the file gets truncated when it is opened. Here's some sample code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  int main() {
      std::ofstream file("output.bin", std::ios::binary | std::ios::ate);
  
      std::streamoff orig_offset = file.tellp();
      std::cout &lt;&lt; "Offset after opening: " &lt;&lt; orig_offset &lt;&lt; std::endl;
  
      file.seekp(0, std::ios::end);
      std::streamoff end_offset = file.tellp();
      std::cout &lt;&lt; "Offset at end: " &lt;&lt; end_offset &lt;&lt; std::endl;
  
      file &lt;&lt; "Hello World" &lt;&lt; std::endl;
  
      std::streamoff final_offset = file.tellp();
      std::cout &lt;&lt; "Offset after writing: " &lt;&lt; final_offset &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>I would expect every execution to append "Hello World" to the file. However, the file is truncated as soon as it is opened.</p>
  
  <p>What am I doing wrong? If this is a bug in Visual Studio, are there any workarounds?</p>
  
  <p>Edit:
  Every time the program runs, regardless of whether the file exists or already has contents, the program outputs this:</p>
  
  <pre><code>Offset after opening: 0
  Offset at end: 0
  Offset after writing: 12
  </code></pre>