title: What guarantees are there on interleaved reads and writes?
body: <p>When working with a C++ <code>std::iostream</code> (for example, <code>std::fstream</code> or <code>std::stringstream</code>, does the standard guarantee anything about the relationships between reads and writes performed on the same stream?  That is, is it necessarily true that if I write data into a <code>std::fstream</code>, then try reading data out of that stream, I should see the data I've written?  How about for a <code>std::stringstream</code>?  As an example, is this guaranteed to work?</p>
  
  <pre><code>std::stringstream myStream;
  myStream &lt;&lt; "137 Hello 2.71828";
  
  int myInt;
  std::string myString;
  double myDouble;
  
  myStream &gt;&gt; myInt &gt;&gt; myString &gt;&gt; myDouble; // Parse as expected?
  </code></pre>
  
  <p>Or what about this case?</p>
  
  <pre><code>std::fstream myStream("some-file.txt", ios::in | ios::out);
  myStream &lt;&lt; "137 Hello 2.71828";
  
  int myInt;
  std::string myString;
  double myDouble;
  
  myStream &gt;&gt; myInt &gt;&gt; myString &gt;&gt; myDouble; // Parse as expected?
  </code></pre>
  
  <p>I'm asking because I recently developed a networked stream class in which reads and writes do not affect one another (since reads pull from the network and writes send across the network).  That is, writing</p>
  
  <pre><code>myNetworkStream &lt;&lt; "Hi there!" &lt;&lt; endl;
  </code></pre>
  
  <p>writes across the network, while</p>
  
  <pre><code>myNetworkStream &gt;&gt; myValue;
  </code></pre>
  
  <p>reads from the network.  I'm not sure that this behavior is consistent with the general contract for streams.  If I had to guess, one of the following three probably holds:</p>
  
  <ol>
  <li>The <code>iostream</code> contract says nothing about interleaved reads and writes, or</li>
  <li>In general the <code>iostream</code> contract says nothing about interleaved reads and writes, but there are specific previsions in the spec governing how standard types like <code>fstream</code> and <code>stringstream</code> work, or</li>
  <li>The <code>iostream</code> contract does say something about interleaved reads and writes that makes my network stream class violates.</li>
  </ol>
  
  <p>I have a copy of the spec but the section on streams is so dense and cryptic it's all but impossible to follow.  If anyone could clarify exactly how <code>iostream</code>s are supposed to behave when you mix reads and writes, I'd really appreciate it.</p>