title: Write and read array of objects to binary file?
body: <p>I have tried using <code>fstream</code> and although apparently compiled, the program crash.
  Please tell me what is wrong in above code and what should I do to correct it</p>
  
  <p>I want to recover  all  elements of <code>miperro5[]</code> from the binary file into a new array <code>miperro6[]</code>. As might fix it?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;Perro.hpp&gt;
  #include &lt;fstream&gt;
  
  using std::cout;
  using std::endl;
  
  int main(int argc, char **argv)
  {
      Perro miperro5[5];
      Perro miperro6[5];
      miperro5[2].nombre = "lucky";
  
      std::ofstream myfile;
      myfile.open("example.bin", std::ios::out | std::ios::app | std::ios::binary);
      for (int i = 0; i &lt; 5; i++){
          myfile.write(reinterpret_cast&lt;char *&gt;(&amp;miperro5[i]), sizeof(Perro));
      }
      myfile.close();
  
      std::ifstream myfile2;
      myfile2.open("example.bin", std::ios::in | std::ios::binary);
      for (int j = 0; j &lt; 5; j++){
          myfile2.read(reinterpret_cast&lt;char *&gt;(&amp;miperro6[j]), sizeof(Perro));
      }
      myfile2.close();
  
      for (int z = 0; z &lt; 5; z++){
          cout &lt;&lt; miperro6[z].nombre &lt;&lt; endl;
      }
  
      std::cin.get();
      return 0;
  }
  </code></pre>
  
  <p>This is Perro.hpp</p>
  
  <pre><code>#ifndef PERRO_HPP
  #define PERRO_HPP
  #include &lt;string&gt;
  class Perro
  {
  public:
      Perro();
      ~Perro();
      void ladrar();
      void comer();
      std::string nombre;
      int edad;
      int arra[2];
  
  
  };
  
  #endif // PERRO_HPP
  </code></pre>