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 {

    int ID;
    string name;
    string address;
    string phonenum;
    double years;
        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];
set<Employee > employeeSet;
set<Employee > :: const_iterator iter; // set up an iterator

iter = employeeSet.begin();
while ( iter != employeeSet.end()){
    Employee x = *iter;
cout << x.name << '\n';
}//end while

ifstream inStream;
ofstream outStream;
    string fileIn  = "/Data.txt";
    string fileOut = "/OutData.txt";
        if (outStream.fail())
            cout<< "outStream failed for file: "<< fileOut <<endl;

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;
          if (inStream.fail())
                cout<< "inStream failed for file: "<< fileIn <<endl;
       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];
            inStream.getline(buffer, MAX);
            cout<< buffer <<endl;
          }//end while
return 0;
}//end main()


you cannot output objects to a stream the way you do, no matter if you want them otput as binary or text data. The problem is that most of your members are dynamicaly allocated and are on the heep so you cannot just output the object to the stream because you are otputting the pointers to the data instead of the data it self. You have to ouput each member variable separately and you have to ouput their data not the pointers. I asume also that you'd like to output the objects in text form, for this you'll have to convert the members ID and years to text.
Here is what you could do for each signle object (this is an aproach using new lines as delimiters):


std::stringstream ss;

ss << e1.ID;
outStream << ss.str() << std::endl << e1.name.c_str() << std::endl 
          << e1.address.c_str() << std::endl << e1.phonenum.c_str() << std::endl;
ss << e1.years;
outStream << ss.str() << std::endl;


This of course is not the cleanest or finest solution. In your place I would write my own serialization (this is what this is called) method which would do what I have written above and than I would call it each time I need to write down my object to a file. You could even overload the << opertator.
Also, google c++ class serialization. There is lot of good stuff written on the net about this subject.



Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.