title: Unexpected behavior of fstream binary write/read, unable to read valid values from file
body: <p>To write to file, I use the following function:</p>
  <pre><code>void writeDBToFile_BinaryMode(std::vector&lt;structDB::citizen&gt; dataBase,
          std::string fileName) {
  
      std::fstream fileToWriteTo;
  
      fileToWriteTo.open(fileName, std::ios::binary | std::ios::out);
  
      for (auto &amp;citizen : dataBase) {
          if (citizen.fullName == dataBase[dataBase.size() - 1].fullName
                  &amp;&amp; citizen.address.street
                          == dataBase[dataBase.size() - 1].address.street
                  &amp;&amp; citizen.address.houseNumber
                          == dataBase[dataBase.size() - 1].address.houseNumber
                  &amp;&amp; citizen.address.flatNumber
                          == dataBase[dataBase.size() - 1].address.flatNumber
                  &amp;&amp; citizen.age == dataBase[dataBase.size() - 1].age
                  &amp;&amp; citizen.gender == dataBase[dataBase.size() - 1].gender) {
  
              std::string currentCitizenEntry = citizen.fullName + ';'
                      + citizen.address.street + ';'
                      + std::to_string(citizen.address.houseNumber) + ';'
                      + std::to_string(citizen.address.flatNumber) + ';'
                      + citizen.gender + ';' + std::to_string(citizen.age);
  
              unsigned short int sizeOfLine = currentCitizenEntry.size();
  
              fileToWriteTo.write(reinterpret_cast&lt;char*&gt;(&amp;sizeOfLine),
                      sizeof(sizeOfLine));
  
              fileToWriteTo.write(reinterpret_cast&lt;char*&gt;(&amp;currentCitizenEntry),
                      sizeof(currentCitizenEntry));
  
          } else {
              std::string currentCitizenEntry = citizen.fullName + ';'
                      + citizen.address.street + ';'
                      + std::to_string(citizen.address.houseNumber) + ';'
                      + std::to_string(citizen.address.flatNumber) + ';'
                      + citizen.gender + ';' + std::to_string(citizen.age) + '\n';
  
              unsigned short int sizeOfLine = currentCitizenEntry.size();
  
              fileToWriteTo.write(reinterpret_cast&lt;char*&gt;(&amp;sizeOfLine),
                      sizeof(sizeOfLine));
  
              fileToWriteTo.write(reinterpret_cast&lt;char*&gt;(&amp;currentCitizenEntry),
                      sizeOfLine);
          }
  
      }
  
      fileToWriteTo.close();
  
  }
  </code></pre>
  <p>And to read from file, I use this function:</p>
  <pre><code>void readDBFromFile_BinaryMode(std::vector&lt;structDB::citizen&gt; &amp;dataBase,
          std::string fileName) {
  
      std::ifstream fileToReadFrom;
  
      fileToReadFrom.open(fileName, std::ios::binary);
  
      while (!fileToReadFrom.eof()) {
          unsigned short int sizeToRead;
  
          fileToReadFrom.read(reinterpret_cast&lt;char*&gt;(&amp;sizeToRead),
                  sizeof(sizeToRead));
  
          std::string currentLine;
  
          fileToReadFrom.read(reinterpret_cast&lt;char*&gt;(&amp;currentLine), sizeToRead);
  
          std::cout &lt;&lt; sizeToRead &lt;&lt; std::endl;
          std::cout &lt;&lt; currentLine &lt;&lt; std::endl &lt;&lt; std::endl;
  
      }
  
      fileToReadFrom.close();
  }
  </code></pre>
  <p>The following function are used in context of:</p>
  <pre><code>    libdb::writeDBToFile_BinaryMode(dataBase, &quot;writetofilebinary.txt&quot;);
  
      libdb::readDBFromFile_BinaryMode(dataBase, &quot;writetofilebinary.txt&quot;);
  </code></pre>
  <p>However, output of the read function is not an expected valid value that was written to the file, but semi-correct data mixed with &quot;junk&quot; data, such as:</p>
  <pre><code>0
  Uɥ[ownsend;Highland Drive;5;156;Female
  </code></pre>
  <p>And at the end of execution, error is returned:</p>
  <pre><code>free(): double free detected in tcache 2
  Aborted
  </code></pre>
  <p>Exactly what is preventing my code from running in expected way?</p>