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 






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)
    cin >> choice;
    switch (choice)
    case 1 :
    employee = ReadEmployee();
    case 2 :
    //other caes
    case 10: exit(0)

    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….


Leave a Reply