title: C++ Binary file reading
body: <p>I'm trying to keep objects including vectors of objects in a binary file.</p>
  
  <p>Here's a bit of the load from file code:</p>
  
  <pre><code>template &lt;class T&gt; void read(T* obj,std::ifstream * file) {
      file-&gt;read((char*)(obj),sizeof(*obj));
      file-&gt;seekg(int(file-&gt;tellg())+sizeof(*obj));
  }
  
  void read_db(DB* obj,std::ifstream * file) {
      read&lt;DB&gt;(obj,file);
      for(int index = 0;index &lt; obj-&gt;Arrays.size();index++) {
          std::cin.get(); //debugging
          obj-&gt;Arrays[0].Name = "hi"; //debugging
          std::cin.get(); //debugging
          std::cout &lt;&lt; obj-&gt;Arrays[0].Name;
          read&lt;DB_ARRAY&gt;(&amp;obj-&gt;Arrays[index],file);
          for(int row_index = 0;row_index &lt; obj-&gt;Arrays[index].Rows.size();row_index++) {
              read&lt;DB_ROW&gt;(&amp;obj-&gt;Arrays[index].Rows[row_index],file);
              for(int int_index = 0;int_index &lt; obj-&gt;Arrays[index].Rows[row_index].i_Values.size();int_index++) {
                  read&lt;DB_VALUE&lt;int&gt;&gt;(&amp;obj-&gt;Arrays[index].Rows[row_index].i_Values[int_index],file);
              }
          }
      }
  }
  </code></pre>
  
  <p>And here's the DB/DB_ARRAY classes</p>
  
  <pre><code>class DB {
  public:
      std::string Name;
      std::vector&lt;DB_ARRAY&gt; Arrays;
      DB_ARRAY * operator[](std::string);
      DB_ARRAY * Create(std::string);
  };
  class DB_ARRAY {
  public:
      DB* Parent;
      std::string Name;
      std::vector&lt;DB_ROW&gt; Rows;
      DB_ROW * operator[](int);
      DB_ROW * Create();
      DB_ARRAY(DB*,std::string);
      DB_ARRAY();
  };
  </code></pre>
  
  <p>So now the first argument to the read_db function would have correct values, and the vector Arrays on the object has the correct size, However if I index any value of any object from obj->Arrays it's going to throw the access violation exception. </p>
  
  <pre><code>std::cout &lt;&lt; obj-&gt;Arrays[0].Name; // error
  std::cout &lt;&lt; &amp;obj-&gt;Arrays[0]; // no error
  </code></pre>
  
  <p>The later always prints the same address,  so when I save an object casted to char* does it save the address of it too?</p>