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

************************
edelossantosAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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_
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;    
}
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;
   
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
edelossantosAuthor Commented:
Thank you to all. Del
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.