title: C++ Program crashes when reading object from random access file
body: <p>I have the following User.h that holds several attributes (strings). User.cpp has all the definitions.</p>
  
  <pre><code>//User.h
  #ifndef USER_H
  #define USER_H
  #include&lt;iostream&gt;
  #include &lt;cstring&gt;
  
  using namespace std;
  
  class User{
  
    string username;
  
    public:
           User();
           string getUsername() const;                      
           void setUsername(string);
  
  };
  #endif
  </code></pre>
  
  <p>I'm using a another class, "File" to insert new users/view users from a randomly accessed .dat file</p>
  
  <pre><code>//File.h
  #ifndef FILE_H
  #define FILE_H
  #include "User.h"
  
  class File{
      public:
  
               void loadUser(); 
               bool addUser(User&amp;); 
  
  };
  #endif
  </code></pre>
  
  <p>File class definitions</p>
  
  <pre><code>//File.cpp
  #include&lt;cstring&gt;
  #include&lt;iostream&gt;
  #include&lt;iomanip&gt;
  #include&lt;fstream&gt;
  
  #include "User.h"
  #include "File.h"
  
  using namespace std;
  
  User tempUser;
  fstream usersFile;
  
  void File::loadUser(){
       usersFile.open("users.dat", ios::in | ios::binary);
  
       usersFile.seekg(0);  
  
       // commenting the following lines prevented the issue
       usersFile.read(reinterpret_cast&lt;char *&gt;(&amp;tempUser), sizeof(tempUser)); 
  
       cout&lt;&lt;tempUser.getUsername().c_str(); 
  
       usersFile.close();
  }
  
  bool File::addUser(User&amp; user){
  
       usersFile.open("users.dat", ios::out | ios::ate | ios::binary);
  
       // no issue when writing to file
       usersFile.write( reinterpret_cast&lt;const char *&gt;(&amp;user), sizeof(user));
  
       usersFile.close();
  
       cout&lt;&lt;"User added";  
  }
  </code></pre>
  
  <p>I'm getting the above mentioned issue when running. No compilation issue though.</p>
  
  <p>Is there any issue when dealing with objects that has "string attributes" inside, in handling?</p>
  
  <p>Please help</p>