title: Protocol Buffers; saving data to disk & loading back issue
body: <p>I have an issue with storing Protobuf data to disk.
  The application i have uses Protocol Buffer to transfer data over a socket (which works fine), but when i try to store the data to disk it fails.
  Actually, saving data reports no issues, but i cannot seem to load them again properly.
  Any tips would be gladly appreciated.</p>
  
  <pre><code>void writeToDisk(DataList &amp; dList)
  {
      // open streams
      int fd = open("serializedMessage.pb", O_WRONLY | O_CREAT);
      google::protobuf::io::ZeroCopyOutputStream* fileOutput = new google::protobuf::io::FileOutputStream(fd);
      google::protobuf::io::CodedOutputStream* codedOutput = new google::protobuf::io::CodedOutputStream(fileOutput);
  
      // save data
      codedOutput-&gt;WriteLittleEndian32(PROTOBUF_MESSAGE_ID_NUMBER); // store with message id
      codedOutput-&gt;WriteLittleEndian32(dList.ByteSize()); // the size of the data i will serialize
      dList.SerializeToCodedStream(codedOutput); // serialize the data
  
      // close streams
      delete codedOutput;
      delete fileOutput;
  
      close(fd);
  }
  </code></pre>
  
  <p>I've verified the data inside this function, the dList contains the data i expect. The streams report that no errors occur, and that a reasonable amount of bytes were written to disk. (also the file is of reasonable size)
  But when i try to read back the data, it does not work. Moreover, what is really strange, is that if i append more data to this file, i can read the first messages (but not the one at the end).</p>
  
  <pre><code>void readDataFromFile()
  {   
      // open streams
      int fd = open("serializedMessage.pb", O_RDONLY);
      google::protobuf::io::ZeroCopyInputStream* fileinput = new google::protobuf::io::FileInputStream(fd);
      google::protobuf::io::CodedInputStream* codedinput = new google::protobuf::io::CodedInputStream(fileinput);
  
      // read back
      uint32_t sizeToRead = 0, magicNumber = 0;
      string parsedStr = "";
  
      codedinput-&gt;ReadLittleEndian32(&amp;magicNumber); // the message id-number i expect
      codedinput-&gt;ReadLittleEndian32(&amp;sizeToRead); // the reported data size, also what i expect
      codedinput-&gt;ReadString(&amp;parsedstr, sizeToRead)) // the size() of 'parsedstr' is much less than it should (sizeToRead)
  
      DataList dl = DataList();
  
      if (dl.ParseFromString(parsedstr)) // fails
      {
          // work with data if all okay
      }
  
      // close streams
      delete codedinput;
      delete fileinput;
      close(fd);
  }
  </code></pre>
  
  <p>Obviously i have omitted some of the code here to simplify everything.
  As a side note i have also also tried to serialize the message to a string &amp; save that string via CodedOutputStream. This does not work either. I have verified the contents of that string though, so i guess culprit must be the stream functions.</p>
  
  <p>This is a windows environment, c++ with protocol buffers and Qt.</p>
  
  <p>Thank you for your time!</p>