title: Segmentation fault in Data file handling in c++
body: <p>I've written a small code in c++ to understand data file handling. The program consists of accepting entries, writing them to a .dat file and searching for a particular entry. So far, only the writing function is working, the reading and the searching function give a segmentation fault error. What's going wrong?</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;fstream&gt;
  #include&lt;stdlib.h&gt;
  using namespace std;
  class data_base
  {
    string name;
    long int no;
   public:
   void input()
  {
   cout&lt;&lt;"\nEnter name:";
   cin&gt;&gt;name;
   cout&lt;&lt;"Enter ph number:";
   cin&gt;&gt;no;
  }
  void display()
  {
  cout&lt;&lt;name&lt;&lt;"\t"&lt;&lt;no;
  }
  string retname()
  {
   return name;
  }
  long int retno()
  {
   return no;
  }
  };
  void display_all()
  {
    data_base d;
    fstream in;
    in.open("database.dat",ios::in|ios::binary);
    if(!in.is_open())
      cout&lt;&lt;"Error opening file";
    else{
       while(in.read((char *)&amp;d,sizeof(d)))
       {
         d.display();
       }
       }
    in.close();
  }
  void search_name()
  {
     data_base d;
     fstream in;
     string s;
     in.open("database.dat",ios::in|ios::binary);
     if(!in.is_open())
     cout&lt;&lt;"Error opening file";
     else{
          cout&lt;&lt;"\nEnter name to be searched:";
          cin&gt;&gt;s;
          while (in.read((char *) &amp;d,sizeof(d))) {
            if(s==d.retname())
           {
             d.display();
           }
           }
         }
    in.close();
   }
   void search_no()
   {
      data_base d;
      fstream in;
      long int l;
      in.open("database.dat",ios::in|ios::binary);
      if(!in.is_open())
        cout&lt;&lt;"Error opening file";
      else{
          cout&lt;&lt;"\nEnter number to be searched:";
          cin&gt;&gt;l;
          while (in.read((char *) &amp;d,sizeof(d)))
          {
             if(l==d.retno())
             {
                d.display();
             }
          }
  
    }
        in.close();
   }
  int main()
  {
    int ch;
    fstream file;
    data_base d,e;
    string s;
    while(1)
    {
       cout&lt;&lt;"1.Add entry\n2.Search by name\n3.Search by 
       number\n4.Display all entries\5.Exit"&lt;&lt;endl;
       cin&gt;&gt;ch;
       switch (ch) {
           case 1: d.input();
                file.open("database.dat",ios::out|ios::app|ios::binary);
                if(!file.is_open())
                 cout&lt;&lt;"Error opening file";
                else
                {
                  file.write((char *)&amp;d,sizeof(d));
                  cout&lt;&lt;"Entry added!"&lt;&lt;endl;
                }
                file.close();
                break;
          case 2:search_name();
                 break;
          case 3:search_no();
                 break;
  
          case 4:display_all();
                 break;
          case 5: exit(0);
        }
    }
  }
  </code></pre>