Hello,
I am having some difficulties getting my program to write the contents of an object to a file. First off, I am not sure if I should be writing the contents of the the object (e1, e2, e3) to the file or if I should be writing the contents of the map (x1, x2, x3). How would I go about doing this?
This is what I get when in the output file whenever I run the program:
This is line one
d¯»ò@»xH@,¯»ò@Ending line
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <fstream>
#include <cstdlib>
using namespace std;
class Employee {
public:
int ID;
string name;
string address;
string phonenum;
double years;
public:
Employee(){
this->ID = 0;
this->name = "jo doe";
this->address = "N/A";
this->phonenum = "00000000";
this->years = years; // add years
}
Employee( int ID, string name, string address, string phonenum, int years){
this->ID = ID;
this -> name = name;
this-> address = address;
this -> phonenum = phonenum;
this -> years = years; // add years
}
Employee( int ID, char name, char address, char phonenum, int years){
this->ID = ID;
this -> name = name;
this-> address = address;
this -> phonenum = phonenum;
this -> years = years; // add years
}
void display(){
cout << " ID: " <<ID<<" | "
<< " name: "<< name << " | "
<<" address: "<< address<< " | "
<<" phone: " << phonenum << " | "
<<" years: " << years //print years
<< endl;
}//end display
void Employee:: empDisplay(void){
cout<< ID << name<< address<< phonenum<< years<<endl;
}
};
bool operator < (Employee e1, Employee e2){
return( e1.name <= e2.name)? true : false;
}
bool operator == (Employee e1, Employee e2){
return ( e1.name == e2.name)? true : false;
}
int main() {
Employee e1(100, "Bill", "713 Street", "317-8472059", 4);
Employee e2(200, "Jim", "345 Street", "317-9087676", 5);
Employee e3(300, "Gene", "90210 Street", "371-9087676", 7);
map<int, Employee>basicEmployees;
basicEmployees.insert(pair<int, Employee>( e1.ID, e1));
basicEmployees.insert(pair<int, Employee>( e2.ID, e2));
basicEmployees.insert(pair<int, Employee>( e3.ID, e3));
Employee x1 = basicEmployees[100];
Employee x2 = basicEmployees[200];
Employee x3 = basicEmployees[300];
x1.display();
x2.display();
x3.display();
set<Employee > employeeSet;
set<Employee > :: const_iterator iter; // set up an iterator
employeeSet.insert(e1);
employeeSet.insert(e2);
employeeSet.insert(e3);
iter = employeeSet.begin();
while ( iter != employeeSet.end()){
Employee x = *iter;
cout << x.name << '\n';
iter++;
}//end while
ifstream inStream;
ofstream outStream;
string fileIn = "/Data.txt";
string fileOut = "/OutData.txt";
outStream.open("/OutData.txt");
if (outStream.fail())
{
cout<< "outStream failed for file: "<< fileOut <<endl;
exit(1);
}
outStream << "This is line one" <<endl;
outStream.write((char *) & e1, sizeof(e1));
outStream.write((char *) & e2, sizeof(e2));
outStream.write((char *) & e3, sizeof(e3));
outStream << "Ending line";
cout << "ending write to file " << fileOut<<endl;
outStream.close();
inStream.open("/OutData.txt");
if (inStream.fail())
{
cout<< "inStream failed for file: "<< fileIn <<endl;
exit(1);
}
inStream.open("/OutData.txt");
inStream.read((char *) & e1, sizeof(e1));
inStream.read((char *) & e2, sizeof(e2));
inStream.read((char *) & e3, sizeof(e3));
const int MAX = 80;
char buffer [MAX];
while(inStream.good())
{
inStream.getline(buffer, MAX);
cout<< buffer <<endl;
}//end while
inStream.close();
return 0;
}//end main()