Anda di halaman 1dari 6

/*PROGRAM OF DATA HANDILING*/

#include<fstream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> void void void void create(); displayall(); modify(int); delrec(int);

class emp { int eno; char ename[20]; float esalary; public: void accept(); void display(); void modi(); int geteno(); }; void insert(emp); void emp::accept() { cout<<"\nemployee no.:"; cin>>eno; cout<<"employee name:"; cin>>ename; cout<<"employee salary:"; cin>>esalary; } void emp::display() { cout<<"\n\nEmployee no. :"<<eno; cout<<"\nEmployee name :"<<ename; cout<<"\nEmployee salary :"<<esalary; } int emp::geteno() {

return eno; } void main() { char ch, ans; int status=0, no; emp e2; do { clrscr(); cout<<"\n***EMPLOYEE MENU***"; cout<<"\n1. Create file"; cout<<"\n2. Insert Record"; cout<<"\n3. Modify Record"; cout<<"\n4. Delete Record"; cout<<"\n5. Display all Records"; cout<<"\n6. Exit"; cout<<"\nEnter your choice (1-6):-"; cin>>(ch); switch(ch) { case '1':clrscr(); if(status==0) { create(); status=1; } else cout<<"\n!!File already exists!!"; break; case '2':clrscr(); cout<<"Enter employee details to be inserted:-"; e2.accept(); insert(e2); break; case '3':clrscr(); cout<<"\n Enter employee no. to be modified:-";

cin>>no; modify(no); break; case '4' :clrscr(); cout<<"\n Enter employee no. to be deleted:-"; cin>>no; delrec(no); break; case '5':displayall(); break; case '6':exit(0); break; default : cout<<"\n!!Wrong Choice!!"; break; }//switch cout<<"\n\n Back to Employee Menu?(y/n):-"; cin>>ans; }while(ans=='y' || ans=='Y'); } void create() { emp e; char ans; fstream obj("employee.dat",ios::out|ios::binary); do { e.accept(); obj.write( (char *)&e, sizeof(e)); //obj>>"\n"; using this only one record is written to the file; cout<<"\nWant to enter more records(y/n)?:-"; cin>>ans; }while(ans=='y' ||ans=='Y'); }//create

void displayall()

{ emp e; fstream obj("employee.dat", ios::in|ios::binary); if(obj==NULL) { cout<<"\nFile does not exists!!"; return; } clrscr(); while(obj.read((char *)&e, sizeof(e))) { e.display(); } }//display void modify(int no) { emp e; int rec=0,flag=0; fstream obj("employee.dat",ios::in|ios::out|ios::binary); if(obj==NULL) { cout<<"\nFile does not exists!!"; return; } while(obj.read((char *)&e, sizeof(e))) { rec++; if(no==e.geteno()) { cout<<"\nthe record is:"; e.display(); cout<<"\nEnter new details:"; e.accept(); obj.seekg((rec-1)*(sizeof(e)), ios::beg); obj.write((char *)&e, sizeof(e)); flag=1; break; } } if (flag==0) cout<<"record does not exists!";

else cout<<"**Modified**"; }//modify void delrec(int no) { emp e; int flag=0; fstream obj("employee.dat",ios::in|ios::binary); if(obj==NULL) { cout<<"\nFile does not exists!!"; return; } fstream object("temp.dat",ios::out|ios::binary); while(obj.read((char *)&e, sizeof(e))) { if(no!=e.geteno()) object.write((char *)&e,sizeof(e)); else flag=1; } if(flag==0) cout<<"\n!!The record does not exists!!:"; else cout<<"\n!!Record deleted!!"; obj.close(); object.close(); remove("employee.dat"); rename("temp.dat","employee.dat"); }//delete void insert(emp e1) { emp e; int pos=0, ctr=1; fstream obj("employee.dat", ios::in|ios::binary); while(obj.read((char *)&e, sizeof(e))) { pos++; if (e1.geteno()<=e.geteno()) break; }

obj.close(); if(e1.geteno()>e.geteno()) pos++; // obj.seekg(0,ios::beg); obj.open("employee.dat", ios::in|ios::binary); fstream object("temp.dat",ios::out|ios::binary); while(ctr<pos) { obj.read((char *)&e, sizeof(e)); object.write((char *)&e, sizeof(e)); ctr++; } object.write((char *)&e1, sizeof(e1)); while(obj.read((char *)&e, sizeof(e))) { object.write((char *)&e, sizeof(e)); } obj.close(); object.close(); remove("employee.dat"); rename("temp.dat","employee.dat"); }

Anda mungkin juga menyukai