Question
P3-XML-Part 2 CS 3370 Program 3 XML-String and Stream Processing Part Two Employee Database Now you will extend your code from part one and read
P3-XML-Part 2
CS 3370 Program 3 XML-String and Stream Processing
Part Two
Employee Database
Now you will extend your code from part one and read and write your objects to and from a binary file.
Employee Class
Add the following methods to your Employee class:
void write(std::ostream&) const; // Write a fixed-length record to current file position void store(std::iostream&) const; // Overwrite (or append) record in (to) file static Employee* read(std::istream&); // Read record from current file position static Employee* retrieve(std::istream&,int); // Search file for record by id
Note that read and retrieve, return a nullptr if they fail to read valid input or if they have finished reading all of the employees in the file.
Main.cpp
The following is an overview of what your main function should do to test your functions using the file employee.xml (provided in this Zip file):
1) Obtain the name of an XML file to read from the command line (argv[1]). Print an error message and halt the program if there is no command-line argument provided, or if the file does not exist.
2) Read each XML record in the file by repeatedly calling Employee::fromXML, which creates an Employee object on-the-fly, and store it in a vector (I recommend using unique_ptrs in the vector).
3) Now create a new file of fixed-length Employee records. This is explained below. Write the records for each employee to your new file (call it employee.bin) in the order they appear in your vector. Open this file as an fstream object with both read and write capability, and in binary format. 4) Clear your vector in preparation for the next step. 5) Traverse the file by repeated calls to Employee::read, filling your newly emptied vector with Employee pointers for each record read. 6) Loop through your vector and call your 'display' function to output each record in your binary file. 7) Search the file for the Employee with id 12345 using Employee::retrieve. 8) Change the salary for the retrieved object to 150000.00 9) Write the modified Employee object back to file using Employee::store 10) Retrieve the object again by id (12345) and print its salary to verify that the file now has the updated salary. 11) Create a new Employee object of your own with a new, unique id, along with other information. 12) Store it in the file using Employee::store. 13) Retrieve the record with Employee::retrieve and display it to cout.
Implementation Notes
To process fixed-length records in a file requires special processing. Our Employee objects use std::string objects, which are allowed to have strings of any length, but we need to write fixed-length, byte-records to files using ostream::write (and we read them back into memory with istream::read). Some strings may therefore get truncated. Here is the record structure I used for transferring Employee data to and from files:
struct EmployeeRec {
int id; char name[31]; char address[26]; char city[21]; char state[21]; char country[21]; char phone[21]; double salary;
}; Note that the strings here are C-style, zero-delimited strings. You need to copy data from and to an EmployeeRec when doing I/O with the binary file. It is the EmployeeRec object that is actually written/read.
See employeedb.cpp.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started