[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Employee.cpp

Posted on 2005-05-01
4
Medium Priority
?
457 Views
Last Modified: 2008-01-09
>> 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
Comment
Question by:edelossantos
4 Comments
 
LVL 8

Assisted Solution

by:novitiate
novitiate earned 800 total points
ID: 13907130
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
 
LVL 4

Assisted Solution

by:smpoojary
smpoojary earned 400 total points
ID: 13908766
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 800 total points
ID: 13908847
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
 

Author Comment

by:edelossantos
ID: 13912708
Thank you to all. Del
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question