title: Protocol buffers issue, multiple serializations into a binary file
body: <p>I am getting some weird behaviour from protobuf binary file io. I am pre-processing a text corpus into a protobuf intermediary file. My serialization class looks as follows: </p>
  
  <pre><code>  class pb_session_printer
    {
    public:
      pb_session_printer(std::string &amp; filename)
        : out(filename.c_str(), std::fstream::out | std::fstream::trunc | 
                                std::fstream|binary)
        {}
  
      void print_batch(std::vector&lt;session&gt; &amp; pb_sv)
      {
        boost::lock_guard&lt;boost::mutex&gt; lock(m);
  
        BOOST_FOREACH(session &amp; s, pb_sv)
        {
          std::cout &lt;&lt; out.tellg() &lt;&lt; ":";
          s.SerializeToOstream(&amp;out);
          out.flush();
          std::cout &lt;&lt; s.session_id() &lt;&lt; ":" &lt;&lt; s.action_size() &lt;&lt; std::endl;
        }
        exit(0);
      }
  
      std::fstream out;
      boost::mutex m;
    };
  </code></pre>
  
  <p>A snippet of output looks like : </p>
  
  <pre><code>0:0:8
  132:1:8
  227:2:6
  303:3:6
  381:4:19
  849:5:9
  1028:6:2
  1048:7:18
  1333:8:28
  2473:9:24
  </code></pre>
  
  <p>The first field shows that serialization is proceeding as normal. </p>
  
  <p>When I run my loading program :</p>
  
  <pre><code>int main()
  {
    std::fstream in_file("out_file", std::fstream::in | std::ios::binary);
    session s;
  
    std::cout &lt;&lt; in_file.tellg() &lt;&lt; std::endl;
    s.ParseFromIstream(&amp;in_file);
    std::cout &lt;&lt; in_file.tellg() &lt;&lt; std::endl;
    std::cout &lt;&lt; s.session_id() &lt;&lt; std::endl;
  
    s.ParseFromIstream(&amp;in_file);
  }
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>0
  -1
  111
  libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse message of type 
  "session" because it is missing required fields: session_id
  </code></pre>
  
  <p>session_id : 111 is an entry towards the end of the stream, I clearly don't understand the semantics of binary-io facilities of the library. Please help.</p>