title: How can I open a file for reading & writing, creating it if it does not exist, without truncating it?
body: <p>What is the proper set of I/O flags for a <code>std::fstream</code>, where I want to be able to read from <em>and</em> write to the file, without truncating the file if it exists, but creating it if it does not?</p>
  
  <p>I've tried</p>
  
  <pre><code>std::ios::binary | std::ios::in | std::ios::out
  std::ios::binary | std::ios::in | std::ios::out | std::ios::ate
  </code></pre>
  
  <p>but neither of these create the file if it does not already exist.</p>
  
  <p>I don't want <code>std::ios::app</code>, because I also need to be able to seek around the file at will, with both the <em>get</em> and <em>put</em> cursors.</p>
  
  <p>One workaround, I suppose, would be to instantiate an <code>std::ofstream</code> first, then immediately close it and open the stream I really want, but that seems messy if it can be avoided with a single stream object.</p>