0% found this document useful (0 votes)
3 views6 pages

SA3_2101053_DSA4

The document contains a C++ program that manages employee records, allowing users to add, display, search, and delete employee information. It utilizes file handling to store employee data in binary format and maintains an index file for quick access to employee IDs. The program includes a menu-driven interface for user interaction.

Uploaded by

rudrajamdarsits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

SA3_2101053_DSA4

The document contains a C++ program that manages employee records, allowing users to add, display, search, and delete employee information. It utilizes file handling to store employee data in binary format and maintains an index file for quick access to employee IDs. The program includes a menu-driven interface for user interaction.

Uploaded by

rudrajamdarsits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ASSIGNMENT NO.

- 3

NAME-JAMDAR RUDRA RAHUL


STD-SE DIV-A
ROLL NO.-53

INPUT :

#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
class Employee
{
private:
char name[50];
int ID;
char Designation[50];
int Salary;
public:
void get_data()
{
cout << "Enter name of employee: ";
cin.ignore();
cin.getline(name, 50);
cout << "Enter employee Id: ";
cin >> ID;
cout << "Enter designation of employee: ";
cin.ignore();
cin.getline(Designation, 50);
cout << "Enter salary of employee: ";
cin >> Salary;
}
void display()
{
cout << "Name: " << name << endl;
cout << "Id: " << ID << endl;
cout << "Designation: " << Designation << endl;
cout << "Salary: " << Salary << endl;
}
void add()
{
ofstream file1, file2;
Employee E;
E.get_data();
file1.open("Employee.txt", ios::binary | ios::app);
if (!file1) {
cout << "Error opening file Employee.txt!" << endl;
return;

}
file2.open("Indexed.txt", ios::binary | ios::app);
if (!file2) {
cout << "Error opening file Indexed.txt!" << endl;
return;
}
file1.write((char*)&E, sizeof(E));
int num = E.ID;
file2.write((char*)&num, sizeof(num));
file1.close();
file2.close();
}
void read()
{ Employee e;
ifstream datafile, indexfile;
datafile.open("Employee.txt", ios::binary);
indexfile.open("Indexed.txt", ios::binary);
if (!datafile || !indexfile) {
cout << "Error opening files!" << endl;
return;
}
cout << "\n Data from Employee File: " << endl;
while (datafile.read((char*)&e, sizeof(e)))
{ e.display();
}
datafile.close();
cout << "\n Data from Index File: " << endl;
int empID;
while (indexfile.read((char*)&empID, sizeof(int)))
{ cout << "Employee ID: " << empID << endl;
}
indexfile.close();
}

int get_ID()
{
return ID;
}
void search(int ID)
{
ifstream file1;
file1.open("Employee.txt", ios::binary);

if (!file1) {
cout << "Error opening file Employee.txt!" << endl;
return;
}
Employee E;
bool found = false;
while (file1.read((char*)&E, sizeof(E)))
{
if (E.get_ID() == ID)
{
E.display();
found = true;
break;
}
}
if (!found)
cout << "Employee with ID " << ID << " not found." << endl;
file1.close();
}
void delete_employee(int ID)
{
ifstream file1, file3;
ofstream file2, file4;
file1.open("Employee.txt", ios::binary);
if (!file1) {
cout << "Error opening file Employee.txt!" << endl;
return;
}
file2.open("Employeetemp.txt", ios::binary | ios::app);
if (!file2) {
cout << "Error opening file Employeetemp.txt!" << endl;
return;
}
file3.open("Indexed.txt", ios::binary);
if (!file3) {
cout << "Error opening file Indexed.txt!" << endl;
return;
}
file4.open("Indexedtemp.txt", ios::binary | ios::app);
if (!file4) {
cout << "Error opening file Indexedtemp.txt!" << endl;
return;
}
Employee E;
int num;

while (file1.read((char*)&E, sizeof(E)))


{
if (E.get_ID() != ID)
{
file2.write((char*)&E, sizeof(E));
}
}
while (file3.read((char*)&num, sizeof(num)))
{
if (num != ID)
{
file4.write((char*)&num, sizeof(num));
}
}
file1.close();
file2.close();
file3.close();
file4.close();
remove("Employee.txt");
rename("Employeetemp.txt", "Employee.txt");
remove("Indexed.txt");
rename("Indexedtemp.txt", "Indexed.txt");
}
};
int main()
{
Employee E1;
char ch;
int ID;
while (true)
{
cout << "1. Add Employee: " << endl;
cout << "2. Display employees:" << endl;
cout << "3. Search Employee by ID: " << endl;
cout << "4. Delete Employee by ID" << endl;
cout << "5. Exit" << endl;
cout << "Enter your choice: ";
cin >> ch;
switch (ch)
{
case '1':
E1.add();
break;
case '2':
E1.read();
break;

case '3':
cout << "Enter ID to search: ";
cin >> ID;
E1.search(ID);
break;
case '4':
cout << "Enter ID to delete: ";
cin >> ID;
E1.delete_employee(ID);
break;
case '5':
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid choice!" << endl;
}
}
return 0;
}

OUTPUT :

student@student-OptiPlex-3020:~$ g++ SA3_2101053_04.cpp


student@student-OptiPlex-3020:~$ ./a.out
1. Add Employee:
2. Display employees:
3. Search Employee by ID:
4. Delete Employee by ID
5. Exit
Enter your choice: 1
Enter name of employee: Yash
Enter employee Id: 1111
Enter designation of employee: CEO
Enter salary of employee: 10000000
1. Add Employee:
2. Display employees:
3. Search Employee by ID:
4. Delete Employee by ID
5. Exit
Enter your choice: 1
Enter name of employee: Atharva
Enter employee Id: 1108
Enter designation of employee: Manager
Enter salary of employee: 50000
1. Add Employee:
2. Display employees:
3. Search Employee by ID:
4. Delete Employee by ID
5. Exit
Enter your choice: 2

Data from Employee File:


Name: Yash
Id: 1111
Designation: CEO
Salary: 10000000
Name: Atharva Id:
1108
Designation: Manager
Salary: 50000

Data from Index File:


Employee ID: 1111
Employee ID: 1108
1. Add Employee:
2. Display employees:
3. Search Employee by ID:
4. Delete Employee by ID
5. Exit
Enter your choice: 3
Enter ID to search: 1111
Name: Yash
Id: 1111
Designation: CEO
Salary: 10000000
1. Add Employee:
2. Display employees:
3. Search Employee by ID:
4. Delete Employee by ID
5. Exit
Enter your choice: 4
Enter ID to delete: 1108
1. Add Employee:
2. Display employees:
3. Search Employee by ID:
4. Delete Employee by ID
5. Exit
Enter your choice: 2

Data from Employee File:


Name: Yash
Id: 1111
Designation: CEO
Salary: 10000000

Data from Index File:


Employee ID: 1111
1. Add Employee:
2. Display employees:
3. Search Employee by ID:
4. Delete Employee by ID
5. Exit
Enter your choice: 5
Exiting...

You might also like