i made a program in dev c++ to merge contents of two files into a single file but the merged file is showing garbage outputs.
question:-
Write a C++ program to merge the content of two files namely “Employee.txt”
and
“Salary.txt” into “EmployeeDetails.txt”.
Sample contents of Employee.txt
EmployeeName EmployeeNumber Department Age
Rohit 12134 CSE 30
Ajay 13543 ECE 32
Bikash 12345 EEE 40
Sample contents of Employee.txt
GrossSalary NetSalary
12222 10000
22222 20000
32222 30000
Sample contents of the merged file EmployeeDetails.txt
EmployeeName EmployeeNumber Department Age GrossSalary Netsal
Rohit 12134 CSE 30 12222 10000
Ajay 13543 ECE 32 22222 20000
Bikash 12345 EEE 40 32222 30000
#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;
fstream fil1;
fil1.open("empdetails.txt",ios::in|ios::out);
cout<<"\n\n Name \t Emp Num \t Dep \t Age \t Gross Sal \t Net Sal \n";
do
{
fil1.read((char*)&e,sizeof(e));
e.display1();
fil1.read((char*)&s,sizeof(s));
s.display2();
}while(fil1);
}
int main()
{
int n;
emp e1;sal s1;
fstream fil1,fil2,fil3;
fil1.open("emp.txt",ios::in|ios::out);
fil2.open("sal.txt",ios::in|ios::out);
fil3.open("empdetails.txt",ios::in|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));//in these 2 steps i try to
fil3.write((char*)&s1,sizeof(s1));//merge the contents of
// both objects
}
fil1.close();
fil2.close();
fil3.close();
cout<<"\n\n\t\t Merged file contents \n\n\t\t";
display();
getch();
return 0;
}
i am new to file programming.so please help me . i had my oops lab exam next week.thank u.