title: read/write vector of custom objects to/from file
body: <p>I am writting code to write a vector of "person" objects to a file and then read the file. I am getting correct output, but at the end of the main() function iI am getting error " unhandled exception writing location ......"
  What am I doing wrong, how to debug such error ?
  here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  class person{
  public:
      string name;
      int age;
  
  
   person(){
  
   }
   person(string name,int age){
       this-&gt;name= name;
       this-&gt;age = age;
  
   }
  
    virtual void display(){
  
         cout &lt;&lt; name &lt;&lt;" "&lt;&lt; age &lt;&lt; endl;
    }
  
  };
  
  
  
  int main () {
  
  fstream f;
  f.open("amu.txt",ios::in | ios::out | ios::trunc);
  if(f){
      cout&lt;&lt;"file found"&lt;&lt;endl;
  }
  else{
      cout&lt;&lt;"file not found"&lt;&lt;endl;
      terminate();
  }
  
  // creating a vector
  vector&lt;person&gt; per;
  
  //populating with "person" objects
  
  for(int i=0;i&lt;100;i=i+10)
  {
      person amu("ameykamat",i);
      per.push_back(amu);
  }
  
  //wrting objects from vector to the file
  
  for(vector&lt;person&gt;::iterator itr = per.begin(); itr != per.end() ; ++itr)
      {
  
      f.write((char *)&amp;(*itr),sizeof(*itr));
  
      }
  
  // taking curson to the start of the file
  
      f.seekg(0,ios::beg);
  
  //creating temp obj for printing from file
      person amu;
      while(f.read((char *)&amp;amu, sizeof(amu))){
      cout&lt;&lt;(amu.name).c_str()&lt;&lt;" "&lt;&lt;(amu.age)&lt;&lt;endl;
  }
  
  
     f.close();
  
  system("pause");
  
    return 0;
  }
  </code></pre>