Write a C++ Program to write and read Employee Details using File Handling

By | January 5, 2017

C++ Program to write and read Employee Details using File Handling 

 

#include <iostream>
#include <fstream>
#define FILE_NAME "emp.dat"

using namespace std;

//class employee declaration
class Employee {
private : 
	int 	empID;
	char 	empName[100] ;
	char 	designation[100];
	int 	ddj,mmj,yyj;
	int 	ddb,mmb,yyb;
public  :
	//function to read employee details
	void readEmployee(){
		cout<<"EMPLOYEE DETAILS"<<endl;
		cout<<"ENTER EMPLOYEE ID : " ;
		cin>>empID;
		cin.ignore(1);
		cout<<"ENTER  NAME OF THE EMPLOYEE : ";
		cin.getline(empName,100);

		cout<<"ENTER DESIGNATION : ";
		cin.getline(designation,100);
		
		cout<<"ENTER DATE OF JOIN:"<<endl;
		cout<<"DATE : "; cin>>ddj;
		cout<<"MONTH: "; cin>>mmj;
		cout<<"YEAR : "; cin>>yyj;
		
		cout<<"ENTER DATE OF BIRTH:"<<endl;
		cout<<"DATE : "; cin>>ddb;
		cout<<"MONTH: "; cin>>mmb;
		cout<<"YEAR : "; cin>>yyb;
	}
	//function to write employee details
	void displayEmployee(){
		cout<<"EMPLOYEE ID: "<<empID<<endl
		 <<"EMPLOYEE NAME: "<<empName<<endl
		 <<"DESIGNATION: "<<designation<<endl
		 <<"DATE OF JOIN: "<<ddj<<"/"<<mmj<<"/"<<yyj<<endl
		 <<"DATE OF BIRTH: "<<ddb<<"/"<<mmb<<"/"<<yyb<<endl;
	}
};

int main(){
	
	//object of Employee class
	Employee emp;
	//read employee details
	emp.readEmployee();
	
	//write object into the file
	fstream file;
	file.open(FILE_NAME,ios::out|ios::binary);
	if(!file){
		cout<<"Error in creating file...\n";
		return -1;
	}
	
	file.write((char*)&emp,sizeof(emp));
	file.close();
	cout<<"Date saved into file the file.\n";
	
	//open file again 
	file.open(FILE_NAME,ios::in|ios::binary);
	if(!file){
		cout<<"Error in opening file...\n";
		return -1;
	}
	
	if(file.read((char*)&emp,sizeof(emp))){
			cout<<endl<<endl;
			cout<<"Data extracted from file..\n";
			//print the object
			emp.displayEmployee();
	}
	else{
		cout<<"Error in reading data from file...\n";
		return -1;
	}
	
	file.close();	
	return 0;
}

 

OUTPUT ::

 

EMPLOYEE DETAILS
ENTER EMPLOYEE ID : 1
ENTER  NAME OF THE EMPLOYEE : Jon
ENTER DESIGNATION : Student
ENTER DATE OF JOIN:
DATE : 05
MONTH: 01
YEAR : 2017
ENTER DATE OF BIRTH:
DATE : 25
MONTH: 12
YEAR : 1997
Date saved into file the file.


Data extracted from file..
EMPLOYEE ID: 1
EMPLOYEE NAME: Jon
DESIGNATION: Student
DATE OF JOIN: 05/01/2017
DATE OF BIRTH: 25/12/1997

 

3 thoughts on “Write a C++ Program to write and read Employee Details using File Handling

  1. s.b.k:

    i want a solution for this hw :In this assignment, you will write a program to read information about Employees and store the information in a dynamic array of pointers to classes subject to the following conditions and directions.
    1. Use for reading and writing.
    2. Create a Class called Employee that includes the following fields:
    Name: Maximum 128 character. Names can have spaces, so use fgets and fputs.
    Salary: Float value
    Date of Birth: Structure that has 3 values: Year, Day, Month.
    ID: Integer (long)
    Gender: Character’ or ‘F’
    Address: maximum 256 characters. . Address can have spaces, so use fgets and fputs.
    /All of the above fields must be declared as private
    And you must provide public set and get functions for the variables that you need to expose.
    Provide two constructors, one is the default and the second takes the Name, and Date Of birth. In the default constructor, initialize variable to either 0 value or empty string. Except for the date initialize it 1/1/2000. For the second constructor, initialize all the variables that are not passed to the constructors as described for the Default constructor.
    Also Implement the Copy Constructor and override the = Operator. Write the destructor also even if it is empty..
    In addition to the set/get functions, the class must have the following pubic functions:
    ReadEmployees(File *fp):read all fields of the structure from file with given pointer.
    WriteEmployee(File *fp): write all fields of the structure from file with given pointer.
    3. Create a Class called EmpGroup that includes the following fields:
    Employee** Employees; //dynamic array of Employee pointers to Classes.
    int EmployeeCount ;// Keeps track of Number of Employees.
    int MaxSize :// current MaxSize of the Array with initial value of 4.
    Constructor EmpGroup() ; Create default array with MaxSize 4 , EmployeeCount = 0;
    EmpGroup( File *fp);// Read the EmployeeCount from the File pointed to by fp, set MAxSize = EmployeeCount. Read the Emplyees from the File. Make sure the you free the Dynamic Array Emplyees in the Destructor.
    Declare the Variables as private, and provide functions to set and get the EmployeeCount and MaxSize. Do not Allow the Employees variable to be accessed
    You must provide set and get functions for each of the variables that you may need to expose.
    The class should include the following public functions:
    void AddEmpoyee();// Adds an Employee, if the Array is full increase the array size by 4 and, copy the old array to a temp,, remove the old array and rename the new one as Employees.
    void PrintEmployee (char *name);// finds the Employee with given name and print all fields of that employee to screen.
    void deleteEmployee( char *name); //delete Employee of given name
    void Sort(int sort_type =0), if 0 is passed o nothing is passed then sort by name, if 1 is passed sort by salary, if 3 sort by ID.
    void PrintAll() : Prints all Employees to the screen
    void StoreAll(File *fp);// Stores all Employees to file with pointer fp
    void ReadAll(File *fp);// Read all from a file pointed to by fp.
    In read all you will need to delete the Employee** and recreate it from the file.
    Note PrintAll() can just Call StoreAll(stdout).
    4. Your program must be written in 5 files: main.c, employee.h. employee.cpp, empgroup.h, empgroup.cpp. Note: in QT just use Add Class and it will add two files for each class.
    5. In the File “main.cpp”, define the function void PrintMenu() and the main function: PrintMenu() :Prints a menu that shows the following items:
    • 1. Add Employee
    • 2. Print Employee By Name
    • 3. Delete Employee By Name
    • 4. Sort By Name
    • 5. Sort By Salary
    • 6. Print All
    • 7. Store To File // Here prompt the user to enter the file name
    • 8. Read From File // Here prompt the user for File Name
    • Get Number of Employees.
    • 10 Exit
    If the User enters any other choice just ignore it.
    If your program has 2 arguments, then the second argument will be the file name from which the Initial Data is read, if no argument is used, The Emplyees Array shall be empty
    6. Your main Function should have something like this
    File *fp = NULL;
    if(argc > 1) {
    fp = fopen(argv[1],”r”);// if it fails it will return NULl , so fp is still NULL
    }
    if(fp ) EmpGroup(fp); //Emplyess will be read from the file
    else EmpGroup(); // Emplyees is empty, does not have any Emplyee
    int choice;
    Employee *employee;
    int EmployeeCount =0
    while (1)
    {
    PrintMenu();
    cin >> choice;
    switch (choice)
    {
    case 1 :
    employee = ReadEmployee();
    break
    case 2 :
    //code
    break;
    //other caes
    case 10: exit(0)
    }
    }

    Reply
    1. Codez Club Post author

      Bro do it yourself first, if any error occurs then comment that part of code. We can’t create your whole program. Please first try yourself then ask….

      Reply

Leave a Reply