dsa12
dsa12
AIM:
Company maintains employee information as employee ID, name, designation and salary.
Date: 27.05.2023
*/
#include<iostream>
#include<fstream>
#include<cstring>
//Employee Structure
int emp_id;//Employee ID
}Rec;
//Index File
int position;// Index position of Employee record in the actual data file
}Ind_Rec;
class Employee
private:
Rec Records;
Ind_Rec Ind_Records;
public:
Employee();
void Create();
void Display();
void Update();
void Delete();
void Append();
void Search();
};
Employee::Employee()//constructor
Records.emp_id=-1;
strcpy(Records.name,"");
strcpy( Records.designation,"");
Records.salary=0;
void Employee::Create()
{
int i,j;
char ch='y';
fstream seqfile;
fstream indexfile;
i=0;
indexfile.open("IND.txt",ios::in|ios::out|ios::binary);
seqfile.open("EMP.txt",ios::in|ios::out|ios::binary);
do
cin>>Records.emp_id;
cin>>Records.name;
cin>>Records.designation;
cin>>Records.salary;
seqfile.write((char*)&Records,sizeof(Records))<<flush;
Ind_Records.emp_id=Records.emp_id;
Ind_Records.position=i;
indexfile.write((char*)&Ind_Records,sizeof(Ind_Records))<<flush;
i++;
cin>>ch;
}while(ch=='y');
seqfile.close();
indexfile.close();
void Employee::Display()
fstream seqfile;
fstream indexfile;
int n,i,j;
seqfile.open("EMP.txt",ios::in|ios::out|ios::binary);
indexfile.open("IND.txt",ios::in|ios::out|ios::binary);
indexfile.seekg(0,ios::beg);
seqfile.seekg(0,ios::beg);
i=0;
while(indexfile.read((char *)&Ind_Records,sizeof(Ind_Records)))
{ //then display it
cout<<"\nEmp_ID: "<<Records.emp_id;
cout<<"\nName: "<<Records.name<<flush;
cout<<"\nDesination: "<<Records.designation;
cout<<"\nSalary: "<<Records.salary;
cout<<"\n";
seqfile.close();
indexfile.close();
}
//Function to update the Employee record
void Employee::Update()
int pos,id;
int New_emp_id;
char New_name[10];
char New_desig[20];
int New_salary;
cin>>id;
fstream seqfile;
fstream indexfile;
seqfile.open("EMP.txt",ios::in|ios::out|ios::binary);
indexfile.open("IND.txt",ios::in|ios::out|ios::binary);
indexfile.seekg(0,ios::beg);
pos=-1;//initilize position
while(indexfile.read((char *)&Ind_Records,sizeof(Ind_Records)))
break;
if(pos==-1)
{
cout<<"\n The record is not present in the file";
return;
else
//calculating the position of record in seq. file using the pos of ind. file
int offset=pos*sizeof(Rec);
strcpy(Records.name,New_name);//can be updated
strcpy(Records.designation,New_desig);//can be updated
Records.salary=New_salary;//can be updated
seqfile.write((char*)&Records,sizeof(Records))<<flush;
seqfile.close();
indexfile.close();
void Employee::Delete()
int id,pos;
cin>>id;
fstream seqfile;
fstream indexfile;
seqfile.open("EMP.txt",ios::in|ios::out|ios::binary);
indexfile.open("IND.txt",ios::in|ios::out|ios::binary);
seqfile.seekg(0,ios::beg);
indexfile.seekg(0,ios::beg);
pos=-1;
while(indexfile.read((char *)&Ind_Records,sizeof(Ind_Records)))
pos=Ind_Records.position;
Ind_Records.emp_id=-1;
break;
if(pos==-1)
return;
//calculating the position of record in seq. file using the pos of ind. file
int offset=pos*sizeof(Rec);
strcpy(Records.name,"");//Logical deletion
strcpy(Records.designation,"");//Logical deletion
//From index file also the desired record gets deleted as follows
indexfile.write((char*)&Ind_Records,sizeof(Ind_Records))<<flush;
seqfile.seekg(0);
indexfile.close();
seqfile.close();
void Employee::Append()
fstream seqfile;
fstream indexfile;
int pos;
indexfile.open("IND.txt",ios::in|ios::binary);
indexfile.seekg(0,ios::end);
pos=indexfile.tellg()/sizeof(Ind_Records);
indexfile.close();
indexfile.open("IND.txt",ios::app|ios::binary);
seqfile.open("EMP.txt",ios::app|ios::binary);
cout<<"\nEmp_ID: ";cin>>Records.emp_id;
cout<<"\nName: ";cin>>Records.name;
cout<<"\nDesination: ";cin>>Records.designation;
cout<<"\nSalary: ";cin>>Records.salary;
indexfile.write((char*)&Ind_Records,sizeof(Ind_Records))<<flush;
seqfile.close();
indexfile.close();
void Employee::Search()
fstream seqfile;
fstream indexfile;
int id,pos,offset;
cin>>id;
indexfile.open("IND.txt",ios::in|ios::binary);
pos=-1;
while(indexfile.read((char *)&Ind_Records,sizeof(Ind_Records)))
break;
if(pos==-1)
return;
offset=pos*sizeof(Records);
seqfile.open("EMP.txt",ios::in|ios::binary);
if(Records.emp_id==-1)
return;
seqfile.close();
indexfile.close();
int main()
Employee List;
char ans='y';
int choice,key;
fstream out;
out.open("EMP.txt",ios::out);
out.close();
out.open("IND.txt",ios::out);
out.close();
choice=0;
while(choice !=7)
cout<<"\n 7.Exit";
cin>>choice;
switch(choice)
case 1:
List.Create();
break;
case 2:
List.Display();
break;
case 3:
List.Update();
break;
case 4:
List.Delete();
break;
case 5:
List.Append();
break;
case 6:
List.Search();
break;
case 7:
break;
default:
cout<<"Wrong Choice!!";
}
return 0;