1) Make a class that has ALL of the employee details
EmployeeName, EmployeeNumber, Department, Age, GrossSalary, NetSalary
2) create (in the class) a method that returns a string that contains ALL of the employee details (delimited by {tab or comma})
3) Read the Employee.txt into an array, list, vector of EmployeeDetail objects
4) Read the Salary.txt and update each employee in the array with salary information
5) Create an output file and write the contents to the output using the string created by the built-in method in the class.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
What are the cin and cout operations for in the emp class?
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Those operations are expecting user input. You should only read the contents of the file, right?
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Ah!
So in your display() or something like it, you will need to open two files.
Read a line from the first file
Read a line from the second file
concatenate the lines (space delimited)
write the concatenated line to the output display.
write the concatenated line to the output file.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
but what is wrong with my code???
Don't know what to look for. We have no idea why you think the code is wrong. You didn't explain it to us.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Why do you expect:
fil1.write((char*)&e1,sizeof(e1));
to write a human-readable line like your sample indicates? That's one (non-portable) way to write a terse binary record. Instead, for text files, keep it simple:
fil1 << e.name << " " << e.num << " " << e.dep << " " << e.age << endl;
and reverse that to read it back in:
fil1 >> e.name >> e.num >> e.dep >> e.age;
(Note, whitespace (including endl) tells the istream::operator>>() where to find boundaries of "items" and isn't read into any item. Use getline(fil1, lineString) to read an entire line (including whitespace) into a std::string variable.)
Also, if you need to create the files as well as merge them, then make those operations separate. Open a file for writing, fill it, and close it. Open another file for writing, fill it, and close it. Then open both files for reading, and a new file for writing, and perform the merge.
raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82
I can't tell you why fil1.write() and fil1.read() aren't working, without more information. What values are you entering for e1? What file gets generated when you write() it? What is in e1 after you read() it back?
I don't know what your professor meant by "write it via object." To me that means something more like:
class emp
{
...
void write(ofstream & fil) const {
fil << name << " " << num << " " << dep << " " age << endl;
}
void read(ifstream & fil) {
fil >> name >> num >> dep >> age;
}
...
};
so that later you can do:
e1.write(fil1);
e2.read(fil2);
There's nothing wrong with your logic, if it addresses your assignment. However, what you specified isquestion:-
Write a C++ program to merge the content of two files namely “Employee.txt”
and “Salary.txt” into “EmployeeDetails.txt”.
and what you've programmed does not do that, it generates all three files at the same time. I'm just trying to help.
As far as writing out binary files (using read() and write() the way you are so far), I think you need to open the files in "binary" mode. Include flag ios::binary when you open the file. And if the only operation you're performing on a file is writing (or reading), then don't specify the other flag, just ios::out (or ios::in), not both.
raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82
Probably because of when you check for errors on your input stream:
do
{
fil1.read((char*)&e,sizeof(e));
e.display1();
fil1.read((char*)&s,sizeof(s));
s.display2();
} while (fil1);
After you've read the last e and s out of the file, the fil1 stream is still in a valid state, so you repeat your loop one more time. Each read fails, but since there's no data to replace the variable, it maintains the same values it had from the previous read, so prints the same line as previously.
One solution is to test the state of fil1 after reading e (but before printing it), and if it's at EOF or otherwise in an error-state, break; out of the loop at that point.
raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82
raptr_dflo
Practically a Master Poster
602 posts since Aug 2010
Reputation Points: 76
Solved Threads: 82