0

The program creates two files emp.txt and sal.txt.the contents of these two files are finally put in empdetails.txt.everything is working fine,but the display function:- void display() is displaying the last record TWICE.why the last record is displayed twice???

#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;

class emp
{
      int num,age;
      char name[20],dep[5];

      public:
          void getdata()
          {
             cout<<"\n\n  Name   = ";
             cin>>name;
             cout<<"\n Emp Num   = ";
             cin>>num;
             cout<<"\n Department= ";
             cin>>dep;
             cout<<"\n Age       = ";
             cin>>age;
          }
          void display1()
          {  
            cout<<"\n"<<name<<"\t"<<num<<"\t"<<dep<<"\t\t"<<age;
          }  

};

class sal
{
      float gs,ns;
       public:
           void getsal()
           {
             cout<<"\n Gross sal = ";
             cin>>gs;
             cout<<"\n Net sal   = ";
             cin>>ns;
           }
           void display2()
           {
             cout<<"\t"<<gs<<"\t"<<ns;
           }          
};

void display()
{
   emp e;sal s;
   ifstream fil1;

   fil1.open("empdetails.txt",ios::in);

   cout<<"\n\n Name \t Emp Num \t Dep \t Age \t Gross Sal \t Net Sal \n";  

  while(!fil1.eof())
  {
    fil1.read((char*)&e,sizeof(e));
    e.display1();

    fil1.read((char*)&s,sizeof(s));
    s.display2();
  }   
}

int main()
{
    int n;
    emp e1;sal s1;
    ofstream fil1,fil2,fil3;

    fil1.open("emp.txt",ios::out);
    fil2.open("sal.txt",ios::out);
    fil3.open("empdetails.txt",ios::out);

    cout<<"\n How many employee details do you want to enter = ";
    cin>>n;

    cout<<"\n Enter the deatils one by one \n";
    for(int i=0;i<n;i++)
    {
        e1.getdata();
        fil1.write((char*)&e1,sizeof(e1));

        s1.getsal();
        fil2.write((char*)&s1,sizeof(s1));

        fil3.write((char*)&e1,sizeof(e1));
        fil3.write((char*)&s1,sizeof(s1));
    }
    fil1.close();
    fil2.close();
    fil3.close();

    cout<<"\n\n\t\t Merged file contents \n\n\t\t";
    display();
    getch();
    return 0;
} 
Shikhar Deep
  • 253
  • 2
  • 8
  • 19

2 Answers2

0

The EOF state is set when you read BEYOND the last character. So you should check it after a read().

  while(true)
  {
    fil1.read((char*)&e,sizeof(e));
    // do the checking right after a read()
    if (fil1.eof())
      break;
    e.display1();

    fil1.read((char*)&s,sizeof(s));
    s.display2();
  }   
leesei
  • 6,020
  • 2
  • 29
  • 51
0

Don't use file1.eof like that

Please see other post hundreds of answers already there.

Example : this

Fix like follwing :

  while(fil1.read((char*)&e,sizeof(e)))
  {
    //fil1.read((char*)&e,sizeof(e));
    e.display1();

    fil1.read((char*)&s,sizeof(s));
    s.display2();
  } 

Or

If you still want to use eof :-

while(true)
  {
    fil1.read((char*)&e,sizeof(e));
    if (fil1.eof()) //Check here after reading 
      break;
    e.display1();

    fil1.read((char*)&s,sizeof(s));
    s.display2();
  }
Community
  • 1
  • 1
P0W
  • 46,614
  • 9
  • 72
  • 119