Reading and writing to a file in c++
<p>I am trying to write a triple vector to a file and then be able to read back into the data structure afterward. When I try to read the file back after its been saved the first fifty values come out correct but the rest of the values are garbage. I'd be really appreciative if someone could help me out here. Thanks a lot!</p>

<p>File declaration:</p>

<pre><code>    fstream memory_file("C:\\Users\\Amichai\\Pictures\\output.txt", ios::in | ios::out);    
</code></pre>

<p>Save function:</p>

<pre><code>void save_training_data(fstream &amp;memory_file, vector&lt;vector&lt;vector&lt;long double&gt; &gt; &gt; &amp;training_data)
 {
   int sizeI = training_data.size();
   memory_file.write((const char *)&amp;sizeI, sizeof(int));
   for (int i=0; i &lt; sizeI; i++)
   {
       int sizeJ = training_data[i].size();
       memory_file.write((const char *)&amp;sizeJ, sizeof(int));
       for (int j=0; j &lt; sizeJ; j++) 
       {
           int sizeK = training_data[i][j].size();
           memory_file.write((const char *)&amp;sizeK, sizeof(int));
           for (int k = 0; k &lt; sizeK; k++)
           {
               int temp;
               temp = (int)training_data[i][j][k];
               memory_file.write((const char *)&amp;temp, sizeof(int));
           }
       }
   } 
 }
</code></pre>

<p>Read function:</p>

<pre><code>void upload_memory(fstream &amp;memory_file, vector&lt;vector&lt;vector&lt;long double&gt; &gt; &gt; &amp;training_data)
{
     memory_file.seekg(ios::beg);
     int temp=0;
     int sizeK, sizeJ, sizeI; 
     memory_file.read((char*)&amp;sizeI, sizeof(int));
     training_data.resize(sizeI);
     for (int i=0; i &lt; sizeI; i++)
     {
           memory_file.read((char*)&amp;sizeJ, sizeof(int));
           training_data[i].resize(sizeJ);           
           for (int j=0; j &lt; sizeJ; j++)
           {
               memory_file.read((char*)&amp;sizeK, sizeof(int));
               training_data[i][j].resize(sizeK);
               for (int k = 0; k &lt; sizeK; k++)
               {
                    memory_file.read((char*)&amp;temp, sizeof(int));
                    training_data[i][j][k]=temp;
               }
           }
     } 
}
</code></pre>
