DSA Prac12
DSA Prac12
Company maintains employee information as employee ID,name, designation and salary. Allow user to add,
delete information of employee. Display information of particular employee. If employee does not exist an
appropriate message is displayed. If it is, then the system displays the employee details. Use index sequential
file to maintain the data.
*******************************************************************************************
Program:
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
const int MAX_NAME_LENGTH = 50;
const int MAX_DESIGNATION_LENGTH = 50;
struct Employee {
int employeeID;
char name[MAX_NAME_LENGTH];
char designation[MAX_DESIGNATION_LENGTH];
double salary;
};
void addEmployee(fstream& file) {
Employee employee;
cout << "Enter employee ID: ";
cin >> employee.employeeID;
cout << "Enter employee name: ";
cin.ignore();
cin.getline(employee.name, MAX_NAME_LENGTH);
file.close();
tempfile.close();
remove("employee_data.dat");
rename("temp.dat", "employee_data.dat");
if (found) {
cout << "Employee deleted successfully!" << endl;
} else {
cout << "Employee not found!" << endl;
}
file.open("employee_data.dat", ios::in | ios::out |
ios::binary);
}
void displayEmployee(fstream& file) {
int employeeID;
bool found = false;
Employee employee;
cout << "Enter employee ID to display: ";
cin >> employeeID;
file.seekg(0, ios::beg);
while (file.read(reinterpret_cast<char*>(&employee),
sizeof(Employee))) {
if (employee.employeeID == employeeID) {
found = true;
break;
}
}
if (found) {