title: Program crash when reading file the second time in c
body: <h2>Program crash while reading file the second time using c++ file stream</h2>
  
  <p>Hi, guy I was trying to build a Qt Gui application that lets the user inputs all data
  and write it into file using c++ file stream. And when I try to read all the data back from file it work fine for the first time, but when I close the application and try to read it again my application got crush. I try to fix it the best I can, but I couldn't find any solution. Could anyone help help me. Here my source code for reading data from file.</p>
  
  <pre><code>void Form::OpenSlot()
  {
      OpenDialog = new QFileDialog();
      FilePath = OpenDialog-&gt;getOpenFileName(this,"Open",AppDir,"Binary Files(*.bin)");
      QString strID, strName, strSex, strSalary, strHour, strRate, strTotal;
      int row = 0;
      Employee *emp = NULL;
  
      if(!FilePath.isEmpty())
      {
          isNew = false;
          fstream fout(FilePath.toStdString().c_str(),ios::binary|ios::in);
  
          if(fout.fail())
          {
              msg-&gt;setText("Error reading file.\nWrong format.");
              msg-&gt;show();
              fout.close();
              return;
          }
  
          fout.seekg(0,ios::end);
          row = (int)(fout.tellg()/sizeof(Employee));
          fout.seekg(0,ios::beg);
          emp = new Employee[row];
          fout.read((char*)emp,row*sizeof(Employee));
          fout.close();
  
          for(int i=0;i&lt;row;i++)
          {
              DisplayTable-&gt;removeRow(i);
          }
  
          for(int i=0;i&lt;row;i++)
          {
              strID = QString::number((emp+i)-&gt;getID());
              strName = QString::fromStdString((emp+i)-&gt;getName());
              strSex = QString::fromStdString((emp+i)-&gt;getSex());
              strSalary = QString::number((emp+i)-&gt;getSalary());
              strHour = QString::number((emp+i)-&gt;getHour());
              strRate = QString::number((emp+i)-&gt;getRate());
              strTotal = QString::number((emp+i)-&gt;getTotal());
  
              DisplayTable-&gt;insertRow(i);
  
              DisplayTable-&gt;setItem(i,0,new QTableWidgetItem(strID));
              DisplayTable-&gt;setItem(i,1,new QTableWidgetItem(strName));
              DisplayTable-&gt;setItem(i,2,new QTableWidgetItem(strSex));
              DisplayTable-&gt;setItem(i,3,new QTableWidgetItem(strSalary));
              DisplayTable-&gt;setItem(i,4,new QTableWidgetItem(strHour));
              DisplayTable-&gt;setItem(i,5,new QTableWidgetItem(strRate));
              DisplayTable-&gt;setItem(i,6,new QTableWidgetItem(strTotal));
          }
      }
      else
      {
          msg-&gt;setText("Can not find path");
          msg-&gt;show();
      }
  
      delete []emp;
  </code></pre>
  
  <p>}</p>
  
  <p>And here is the source code for writting data into file.</p>
  
  <pre><code>void Form::SaveSlot()
  {
      int row = DisplayTable-&gt;rowCount();
      // Write Data to file
      if(!isNew)
      {
          fstream fin(FilePath.toStdString().c_str(),ios::binary|ios::out);
          Employee *emp = new Employee[row];
  
          for(int i=0;i&lt;row;i++)
          {
               emp-&gt;setID(DisplayTable-&gt;item(i,0)-&gt;text().toInt());
               emp-&gt;setName(DisplayTable-&gt;item(i,1)-&gt;text().toStdString());
               emp-&gt;setSex(DisplayTable-&gt;item(i,2)-&gt;text().toStdString());
               emp-&gt;setSalary(DisplayTable-&gt;item(i,3)-&gt;text().toFloat());
               emp-&gt;setHour(DisplayTable-&gt;item(i,4)-&gt;text().toInt());
               emp-&gt;setRate(DisplayTable-&gt;item(i,5)-&gt;text().toFloat());
               emp-&gt;setTotal(DisplayTable-&gt;item(i,6)-&gt;text().toFloat());
               fin.write((char*)&amp;emp,sizeof(Employee));
          }
          fin.close();
      }
      else
      {
          SaveDialog = new QFileDialog();
          FilePath = SaveDialog-&gt;getSaveFileName(this,"Save",AppDir,"BinaryFile(*.bin)");
  
          if(FilePath.isEmpty())
              return;
  
          fstream fin(FilePath.toStdString().c_str(),ios::binary|ios::out);
          Employee *emp = new Employee[row];
  
          for(int i=0;i&lt;row;i++)
          {
               (emp+i)-&gt;setID(DisplayTable-&gt;item(i,0)-&gt;text().toInt());
               (emp+i)-&gt;setName(DisplayTable-&gt;item(i,1)-&gt;text().toStdString());
               (emp+i)-&gt;setSex(DisplayTable-&gt;item(i,2)-&gt;text().toStdString());
               (emp+i)-&gt;setSalary(DisplayTable-&gt;item(i,3)-&gt;text().toFloat());
               (emp+i)-&gt;setHour(DisplayTable-&gt;item(i,4)-&gt;text().toInt());
               (emp+i)-&gt;setRate(DisplayTable-&gt;item(i,5)-&gt;text().toFloat());
               (emp+i)-&gt;setTotal(DisplayTable-&gt;item(i,6)-&gt;text().toFloat());
          }
  
          fin.write((char*)emp,row*sizeof(Employee));
          fin.close();
      }
  }
  </code></pre>
  
  <p>And here is the Employee class prototype.</p>
  
  <pre><code>class Employee
  {
  public:
      Employee();
      Employee(Employee &amp;emp);
      Employee(int id,string name,string sex,float salary,int hour,float rate,float total);
      void setID(int id);
      void setName(string name);
      void setSex(string sex);
      void setSalary(float salary);
      void setHour(int hour);
      void setRate(float rate);
      void setTotal(float total);
      int getID();
      string getName();
      string getSex();
      float getSalary();
      int getHour();
      float getRate();
      float getTotal();
  private:
      int m_id;
      string m_name;
      string m_sex;
      float m_salary;
      int m_hour;
      float m_rate;
      float m_total;
  };
  </code></pre>