title: Debugging mismatch when writing and reading binary file
body: <p>I have two application, a reader and writer.</p>
  
  <p>The data I'm writing and reading is members of this class:</p>
  
  <pre><code>class MyClass
  {
  public:
     MyClass(){}
     ~MyClass(){}
  public:
     //Getters and setters are removed for simplicity
     void write(std::ofstream&amp; ofile);
     void read(std::ifstream&amp; file);
  private:
     Aenum a= Aenum_0;
  
     double b= 0.0;   
     double c= 0.0;
     double d= 0.0;
     double e= 0.0;
  
     char charArr1[5];
     char charArr2[5];
     char charArr3[5];
     char charArr4[10];
  
     date_and_time time;
  };
  </code></pre>
  
  <p>Where the struct and enum are:</p>
  
  <pre><code>struct date{
         int day = 1;
         int month = 1;
         int year = 2017;
      };
      struct hhmmss{
         int hours = 1;
         int minutes = 1;
         int seconds = 1;
      };
      struct date_and_time {
         date d;
         hhmmss t;
      };
  
      enum Aenum = {Aenum_0, Aenum_1, Aenum_2};
  </code></pre>
  
  <p><strong>The write function:</strong></p>
  
  <pre><code>void MyClass::write(std::ofstream&amp; ofile){
     ofile.write((char*)&amp;a, sizeof(int));                                      
     ofile.write((char*)&amp;b, sizeof(double));                 
     ofile.write((char*)&amp;c, sizeof(double));       
     ofile.write((char*)&amp;d, sizeof(double));                     
     ofile.write((char*)&amp;e, sizeof(double));                 
     ofile.write(&amp;charArr1[0], sizeof(char)*5);   
     ofile.write(&amp;charArr2[0], sizeof(char)*5); 
     ofile.write(&amp;charArr3[0], sizeof(char)*5); 
     ofile.write(&amp;charArr4[0], sizeof(char)*10);     
     ofile.write((char*)&amp;time, sizeof(date_and_time));        
  }
  </code></pre>
  
  <p><strong>The read function:</strong></p>
  
  <pre><code>void MyClass::read(std::ifstream&amp; file){ 
     file.read((char*)&amp;a, sizeof(int));   
     file.read((char*)&amp;b, sizeof(double));                              
     file.read((char*)&amp;d, sizeof(double));                       
     file.read((char*)&amp;d, sizeof(double));                       
     file.read((char*)&amp;e, sizeof(double));                              
     file.read(charArr1, sizeof(char)*5);          
     file.read(charArr2, sizeof(char)*5);             
     file.read(charArr3, sizeof(char)*5);        
     file.read(charArr4, sizeof(char)*10);
     file.read((char*)&amp;time, sizeof(date_and_time));
  }
  </code></pre>
  
  <p>Then in my writer application I do this:</p>
  
  <pre><code>bool writeToBinaryFile(std::vector&lt;myClass&gt;&amp; myClassCollection, const std::string&amp; outputPath){
  
     std::ofstream ofile;
     ofile.open(outputPath);
     if (!ofile.is_open())
        return false;
  
     date_and_time dtnow = getDateAndTimeNow();
     ofile.write((char*)&amp;dtnow, sizeof(date_and_time));                    
  
     int numberOfmyClass = myClassCollection.size();
     ofile.write((char*)&amp;numberOfmyClass, sizeof(int));
  
     for (int i = 0; i &lt; myClassCollection.size(); i++)
        myClassCollection[i].write(ofile);
  
     ofile.close();
     return true;
  }
  </code></pre>
  
  <p>And in the reader I do this:</p>
  
  <pre><code>bool loadData(const std::string&amp; filePath){
  
     std::ifstream file;
     file.open(filePath);
     if (!file.is_open())   
        return false;
  
     date_and_time dtNow;
     int numberOfmyClass;
     file.read((char*)&amp;dtNow, sizeof(date_and_time));
     file.read((char*)&amp;numberOfmyClass, sizeof(int));
  
     for (size_t i = 0; i &lt; numberOfmyClass; i++)
     {
        MyClass myClass;
        myClass.read(file);
        //use data      
     }
     return true;
  }
  </code></pre>
  
  <p>I know that the data in in class members is complete. I have another function where I write to an ASCII file, and there is no missing data.</p>
  
  <p>When I read the binary file, I get correct data for the first 15 (out of 200+) members of the class. Then suddenly the values I'm reading are <em>garbage</em>, as if there are some bytes that shouldn't be there.</p>
  
  <p>So what I'm asking is; am I doing something obviously wrong? And if not, what is the best way to debug such a problem?</p>