title: I am not able to read from the file
body: <p>my question is whenever i try to read from the file both name and roll number,it only read roll number from file,please help me to sort out the problem. i have given my code below.the problem only occurs when i try to write file from one class and read from another.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  class A
  {
      string name;
      int rollno;
  public:
      void setdata();
      void display();
      void Check();
  };
  
  void A::setdata()
  {
      cout&lt;&lt;"enter name"&lt;&lt;endl;
      cin&gt;&gt;name;
      cout&lt;&lt;"enter rollno"&lt;&lt;endl;
      cin&gt;&gt;rollno;
  
  }
  
  void A::display()
  {
      cout&lt;&lt;name&lt;&lt;"    "&lt;&lt;rollno&lt;&lt;endl;
  }
  
  void A::Check()
  {
      A a;
      ifstream file;
      file.open("aa.txt",ios::in);
      while(!file.eof())
      {
          file.read((char*)&amp;a,sizeof(a));
          a.display();
      }
  }
  
  class B
  {
  public:
      void enter();
  
  };
  
  void B::enter()
  {
      A a;
      ofstream file;
      file.open("aa.txt",ios::out);
      a.setdata();
      file.write((char*)&amp;a,sizeof(a));
  }
  
  int main()
  {
      A a1;
      B b1;
      b1.enter();
      a1.Check();
  }
  </code></pre>