#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class employee
{
typedef struct emp
{
int empid;
char name[10];
char designation [10];
double salary;
}emp;
emp rec;
public:
void create();
void display();
int search();
void Delete();
};
void employee::create()
{
char ans;
ofstream fout;
fout.open("emp.txt",ios::out|ios::binary);do
{
cout<<"\n\tEnter EmpId of Employee : ";
cin>>rec.empid;
cout<<"\n\tEnter Name of Employee : ";
cin>>rec.name;
cout<<"\n\tEnter Designation of Employee : ";cin>>rec.designation;
cout<<"\n\tEnter the Salary of Employee : ";
cin>>rec.salary;
fout.write((char *)&rec,sizeof(emp))<<flush;
cout<<"\n\tDo You Want to Add More Records: ";
cin>>ans;
}while(ans=='y'||ans=='Y');fout.close();
}
void employee::display()
{
ifstream fin;
fin.open("emp.txt",ios::in|ios::binary);
fin.seekg(0,ios::beg);
cout<<"\n\tThe Content of File are:\n";
cout<<"\n\tEmpId\tName\tDesignation\tSalary ";
while(fin.read((char *)&rec,sizeof(emp)))
{
if(rec.empid!=-1)
cout<<"\n\t"<<rec.empid<<"\t"<<rec.name<<"\t"<<rec.designation<<
"\t\t"<<rec.salary;
}
fin.close();
}
int employee::search()
{
int r,i=0; ifstream
fin;
fin.open("emp.txt",ios::in|ios::binary);
fin.seekg(0,ios::beg);
cout<<"\n\tEnter the EmpId: "; cin>>r;
while(fin.read((char *)&rec,sizeof(emp)))
{
if(rec.empid==r)
{
cout<<"\n\tRecord Found...\n";
cout<<"\n\tEmpId\tName\tDesignation\tSalary";
cout<<"\n\t"<<rec.empid<<"\t"<<rec.name<<"\t"<<rec.designation<<"\t\t"<<rec.salary;
return i;
}
i++;
}
return -1;
fin.close();
}
void employee::Delete()
{
int pos;
pos=search();
fstream f;
f.open("emp.txt",ios::in|ios::out|ios::binary);
f.seekg(0,ios::beg);
if(pos==-1)
{
cout<<"\n\tRecord Not Found";
return;
}
int offset=pos*sizeof(emp);
f.seekp(offset);
rec.empid=-1; strcpy(rec.name,"NULL");
//rec.div='N';
//strcpy(rec.salary,"NULL"); f.write((char *)&rec,sizeof(emp));f.seekg(0);
f.close();
cout<<"\n\tRecord Deleted";
}
int main()
{
employee obj;
int ch,key; char
ans;
do
{
cout<<"\n\t***** Employee Information *****";
cout<<"\n\t1. Create\n\t2. Display\n\t3. Delete\n\t4.Search\n\t5. Exit";
cout<<"\n\t..... Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1: obj.create();
break;
case 2: obj.display();
break;
case 3: obj.Delete();
break;
case 4: key=obj.search();
if(key==-1)
cout<<"\n\tRecord Not Found...\n";
break;
case 5:
break;
}
cout<<"\n\t..... Do You Want to Continue in Main Menu:";
cin>>ans;
}while(ans=='y'||ans=='Y');
return 1;
}