title: reading/writing class objects to files in c++
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;stdlib.h&gt;
  
  using namespace std;
  
  class Student{
  private:
      char name[40];
      char grade;
      float marks;
  public:
      void getdata();
      void display();
  };
  
  void Student::getdata(){
      char ch;
      cin.get(ch);
      cout&lt;&lt;"Enter name : ";
      cin.getline(name,40);
      cout&lt;&lt;"Enter grade : ";
      cin&gt;&gt;grade;
      cout&lt;&lt;"Enter marks : ";
      cin&gt;&gt;marks;
      cout&lt;&lt;"\n";
  }
  
  void Student::display(){
      cout&lt;&lt;"Name : "&lt;&lt;name&lt;&lt;"\t";
      cout&lt;&lt;"Grade : "&lt;&lt;grade&lt;&lt;"\t";
      cout&lt;&lt;"Marks : "&lt;&lt;marks&lt;&lt;"\t"&lt;&lt;"\n";
  }
  
  int main(){
      system("cls");
      Student arts[3];
      fstream f;
      f.open("stu.txt",ios::in|ios::out|ios::binary);
      if(!f){
          cerr&lt;&lt;"Cannot open file !";
          return 1;
      }
      for(int i=0;i&lt;3;i++){
          arts[i].getdata();
          f.write((char*)&amp;arts[i],sizeof(arts[i]));
      }
      f.seekg(0); //The question is about this statement;
      cout&lt;&lt;"The contents of stu.txt are shown below : \n";
      for(int j=0;j&lt;3;j++){
          f.read((char*)&amp;arts[j],sizeof(arts[j]));
          arts[j].display();
      }
      f.close();
      return 0;
  }
  </code></pre>
  
  <p>The above program reads and writes objects of Student from/to the file "stu.txt".
  It runs fine. But it runs fine even if I switch off the fin.seekg(0) statement. I don't understand this part ? Are we not supposed to set the file pointer to the beginning of the file - before starting to read objects from the file(in the context of this particular program)?.</p>