-
merging two files
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
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;
}
};
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.
-
You're doing some major things wrong...
You shouldn't be writing to either the "emp.txt" or "sal.txt" files. Actually it says that these should be called “Employee.txt” and “Salary.txt”. Those files should already exist and be read from only.
Nothing in this assignment calls for the use of cin or cout. It does not actually ask you to display anything. It could of course print out what it's doing if you really wanted "open files", "merging" "closing files" etc, and maybe notify the user of any errors like file not found. But I would not output the data on screen.
You do not need to even use data types like float for this. Just treat all the data on each line as a string. That way the code for combining the header rows is the same as the code from combining the other rows.
I would not even create classes for this. It's simple enough that there's no benefit in doing so.
-
man i assume emp.txt(rather employee.txt) and same for the other one and i think you have only read the question not my code.pls read that also.i want to create that 2 files also by taking user input and then merge them into employeedetails.txt.
-
-
i posted in other forum because i am not getting answers here.simple yaar
-
Of course I read your code. I also read what you have been asked to do and the code does not match what you have been asked to do.
It asks you to merge two files, it does not ask you to read in input and create those files first. They should exist before the program is run. Create them in a basic text editor like notepad first.
You will get what looks like garbage out until you listen to what I've said and do what the assignment is actually asking of you. I'm not going to tell you which one of the things I've already mentioned is causing this just yet.
I told you most of what you have done wrong. That is your answer. Don't go saying you're not getting any answers.
You either post some updated code that uses some of the advice given, or post more of your asignment sheet that says you have to do it this way, or I'm done here.
-
Oh, it's here too. Has gone to two pages there.
-
to rags_to_riches:what's ur problem man .should i have to take permission from u to write it in other forum
-
No, it's just being polite How To Ask Questions The Smart Way
Moving from one forum to another sequentially - say a couple of days of inactivity is fine.
Concurrently playing off multiple forums against one another is a fine way to ........ everyone who might bother to help you.
See also "getting your answer by successive approximation", where you post some half attempt on a forum, get a few improvements and then claim the whole thing is your own work on another forum. This is repeated until the whole project is done.
-
to salem: i am doing so because i don't have much time . after two days i have my term end lab exams in my college.that's why i am a bit of hurry.
-
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??
-
-
Okay well I'll say this. Because you're writing the data as binary, including types like int and float, parts of the binary file will look like garbage in a text editor.
The reason for opening the files that already exist on disk is that you then start off with something that is human-readable, and thus you're forced to realise that you must treat the data as text, parsing it if you have to, and then again writing it out as text just like when it was read in.
Besides, without a matching binary file reader, you have no way to know if what looks like garbage is in fact correct.
You can read it in from input if you really want, but you should still write it to file and read it back from that file. So unless you want to make even more work for yourself, I recommend just creating the files manually and then reading them in with your program.