to iMalc:
look don't be so angry.my sir said that if you want you can create those two files
(emp.txt and sal.txt) in your program by taking user inputs. so i did that.don't look the
question so strictly(like you said file should be employee.txt; i assume it to be emp.txt
and same for the other one).what i have done in this program,take input from user for different employees
and write it them to two different files and merge these contents into third one.(little bit modify the question)
now(1) what i received suggesstions from other forums according to that i changed all the fileobjects in main()
to ofstream type and in display() ifstream and the file mode ios::binary is used.now this works but the last employee
details is printed twice.why???//i solved this(from other forums) they said that before displaying check for EOL.leave this one
secondly;i made other program also which first create two files and and merge their contents into
third one after all the input work is over for the first two files.surprisingly,this one works.
(2)but why the first logic is not working
Code:
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));//why merging is not taking place here??
fil3.write((char*)&s1,sizeof(s1));//
}
(3)also in the second program i have made objects' array(emp e1[10] and sal s1[10])butif i
try to write and read more than one employee details using single object in this program then
in the output only the last employee details come.why??
here is the second program
Code:
#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;
}
};
int main()
{
int n;
emp e1[10];sal s1[10];
fstream fil1,fil2,fil3;
fil1.open("emp.txt",ios::in|ios::out);
fil2.open("sal.txt",ios::in|ios::out);
fil3.open("employeedetails.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++) //first taking inputs for both the files(emp and sal)
{
e1[i].getdata();
fil1.write((char*)&e1[i],sizeof(e1[i]));
s1[i].getsal();
fil2.write((char*)&s1[i],sizeof(s1[i]));
//fil3.write((char*)&e1[i],sizeof(e1[i])); merging is not done here in this program
//fil3.write((char*)&s1[i],sizeof(s1[i]));
}
for(int i=0;i<n;i++) //merging the contents into third file
{
fil1.read((char*)&e1[i],sizeof(e1[i]));
fil2.read((char*)&s1[i],sizeof(s1[i]));
fil3.write((char*)&e1[i],sizeof(e1[i]));
fil3.write((char*)&s1[i],sizeof(s1[i]));
}
for(int i=0;i<n;i++) //displaying the contents of the third file
{
fil3.read((char*)&e1[i],sizeof(e1[i]));
e1[i].display1();
fil3.write((char*)&s1[i],sizeof(s1[i]));
s1[i].display2();
}
fil1.close();
fil2.close();
fil3.close();
getch();
return 0;
}
(4)and the last doubt --when i open the folder in which my program is saved i found no files.so where are the 3 files but this program
is working properly??