• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 468
  • Last Modified:

Employee.cpp

>> Need checked, please advise.  Del

#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
   
    struct employee {
         char name[64];
         int age;
         int sex;
         int phone;
         float salary;
    } worker = {"peter", 2, 25000};

    ofstream emp_file("employee.DAT", ios::app);

    emp_file.write((char *) &worker, sizeof(employee));

    ifstream mp_file("employee.DAT");
    while (!mp_file.eof())
    {
    mp_file.read((char *) &worker, sizeof(employee));

    cout << "Name: " << worker.name << endl;
      cout << "Sex: " << worker.sex << endl;
    cout << "Age: " << worker.age << endl;
      cout << "Phone: " << worker.phone << endl;
    cout << "Salary: " << worker.salary << endl;
    }
   
    return 0;

}

**********************
employee.DAT
**********************

Name: Enrique (Del) De Los Santos

Sex: Male

Age: 41

Phone: USA-COMP

************************
0
edelossantos
Asked:
edelossantos
3 Solutions
 
novitiateCommented:
well you can also try this.
#include <iostream>
#include <fstream>

using namespace std;
struct employee {
      char name[64];
      int age;
      int sex;
      int phone;
      float salary;
};

ostream & operator << (ostream & os, employee & e)
{
      os.write((char *) &e, sizeof(employee));
      return os;
}
istream & operator >> (istream & is, employee & e)
{
      is.read((char *) &e, sizeof(employee));
      return is;
}
int main(void)
{
   
    employee worker = {"peter", 2, 25000};
      
    ofstream emp_file("employee.dat", ios::app );
      
    emp_file << worker;
      
    ifstream mp_file("employee.dat");
    while (!mp_file.eof())
    {
            mp_file >> worker;
            
            cout << "Name: " << worker.name << endl;
            cout << "Sex: " << worker.sex << endl;
            cout << "Age: " << worker.age << endl;
            cout << "Phone: " << worker.phone << endl;
            cout << "Salary: " << worker.salary << endl;
    }
   
    return 0;
      
}

_novi_
0
 
smpoojaryCommented:
Something like this. Add your own idea. I have written following code in a hurry. I didn't compile it also.
//employee.h
#ifndef __EMPLOYEE_H
#define __EMPLOYEE_H
#include <iostream>
#include <fstream>
using namespace std;

const unsigned int ERR_UNOPEN = 1;
const unsigned int ERR_UNCLOSE = 2;
const unsigned int ERR_UNCREATE = 3;
class employee {
     char name[64];
     int age;
     int sex;
     int phone;
     float salary;
int nTotalRec;  //Total number of records
bool DataBaseOpened;
ifstream mp_file;
public:
      employee();
      unsigned int CreateDatabase();
      unsigned int InitializeDatabase();
      unsigned int UpdateDataBase();
      unsigned int AddEmployee();
      unsigned int DeleteEmployee();
      void DisplayDataBase();
friend ostream & operator << (ostream & os, employee & e);
friend istream & operator >> (istream & is, employee & e);
employee& operator = (employee &e);
~employee();
};

#endif //__EMPLOYEE_H

//employee.cpp
employee::employee()
{
      //Initialize member variables
      memset(name,0,64);
      age = sex = phone = 0;
      salary = 0.0;
      nTotalRec = 0;
DataBaseOpened = false;
}

unsigned int employee::CreateDataBase(int nNoRec;)
{
ofstream emp_file("employee.DAT", ios::out | ios::trunc);
if(!emp_file.is_open())
{
      return ERR_UNCREATE;
}
//Ask user to how many records you want to add
//This argument should come as argument to this function  from main()
i=0;
while(i<nNoRec)
{
      //read data from console
emp_file.write((char *) &worker, sizeof(employee));
emp_file.flush();
++i;
}
nTotalRec = nNoRec;
emp_file.close();
DataBaseOpened = false;
return 0;     //0 is SUCCESS
}

unsigned int employee::InitializeDataBase()
{
      mp_file("employee.DAT");
      if(!mp_file.is_open())
      {
            return ERR_UNOPEN;
      }
      DataBaseOpened = true;
      return 0;
}

void employee::DisplayDataBase()
{
if(!DataBaseOpened) return;
      mp_file.clear();
  while (!mp_file.eof())
    {
           mp_file >> worker;
          if(mp_file.gcount() == 0) break;
cout << worker;
    }
}
ostream & operator << (ostream & os, employee & e)
{
    os << "Name: " << e.name << endl;
     os << "Sex: " << e..sex << endl;
    os << "Age: " << e.age << endl;
    os << "Phone: " << e.phone << endl;
    os << "Salary: " << e.salary << endl;

     return os;
}
istream & operator >> (istream & is, employee & e)
{

     is.read((char *) &e, sizeof(employee));
     return is;
}

employee::~employee()
{
if(!DataBaseOpened) mp_file.close();
//Some other stuff
}
int main(void)
{
employee empDataRec;
do
{
cout << “1-->CreateDataBase()” <<endl;
      cout << “2-->InitializeDatabase()” << endl;
      cout << “3-->UpdateDataBase()” << endl;
      cout << “4-->AddEmployee()”;
      cout << “5-->DeleteEmployee()”;
      cout <<”6-->DisplayDataBase()”;
      cout <<”Choose your number : “;
      cin >> nCh;
      switch(nCh
      {
      case 1:
            break;
      case 2:
            break;
      …
      }
}while(nCh <=6);
return 0;    
}
0
 
itsmeandnobodyelseCommented:
You need to use binary file type. And the worker variable isn't defined properly as you didn't care of number and order of data members.

Regards, Alex

#include <iostream>
#include <fstream>

using namespace std;

int main(void)
{
   
    struct employee {
        char  name[64];
        int   age;
        char  sex;
        int   phone;
        float salary;
    } worker = {"peter", 30, 'm', 1234567, 25000};
   
    ofstream emp_file("employee.DAT", ios::out | ios::binary);

    emp_file.write((char *) &worker, sizeof(employee));
   
    emp_file.close();
   
    ifstream mp_file("employee.DAT", ios::in | ios::binary );
    while (mp_file.read((char *) &worker, sizeof(employee)))
    {
        cout << "Name: "    << worker.name << endl;
        cout << "Age: "     << worker.age << endl;
        cout << "Sex: "     << worker.sex << endl;
        cout << "Phone: "   << worker.phone << endl;
        cout << "Salary: "  << worker.salary << endl;
    }
   
    mp_file.close();
   
    return 0;
   
}

0
 
edelossantosAuthor Commented:
Thank you to all. Del
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now