You are on page 1of 12

[ Binary


Session Objectives
Explain Binary file
Random access File modes Modifying,Deleting,viewing record in a file

#include<iostream.h> #include<fstream.h> #include<conio.h> #include<stdio.h> class employee { private: int eno; char ename[20]; float basic,pf,hra,net; public: void new_employee(); void del_employee(); void show_employee(); void modify_employee(); }; void employee::new_employee() { fstream f char wish; int lastno;

do {"emp.dat",ios::in|ios::binary); f.seekg(0,ios::end); if(f.tellg()==0) lastno=1; else { f.seekg(f.tellg()-sizeof(employee),ios::beg); *)this,sizeof(employee)); lastno=eno+1; } f.seekg(0,ios::end); eno=lastno; cout<<"Employee number:"<<eno<<endl; cout<<"Enter the name:"; cin>>ename; cout<<"Enter basic:"; cin>>basic; cout<<"Enter pf:"; cin>>pf;

cout<<"Enter hra:"; cin>>hra; net=basic+pf+hra; f.write((char *)this,sizeof(employee)); f.close(); cout<<"Wish to record another employee:[y/n]:"; cin>>wish; } while(wish=='y'); } void employee::del_employee() { fstream f1,f2; char wish; int no,found; do { found=0;"emp.dat",ios::in);"temp.dat",ios::out); cout<<"Enter the employee number to be deleted:"; cin>>no; *)this,sizeof(employee));

} while(wish=='y'); } void employee::show_employee() { fstream f; int no,found; char wish;

while(!f1.eof()) { if(eno==no) found=1; else f2.write((char *)this,sizeof(employee)); *)this,sizeof(employee)); } if(!found) cout<<"record not found"; f1.close(); f2.close(); remove ("emp.dat"); rename("temp.dat","emp.dat"); cout<<"wish to record another employee :[y/n]:"; cin>>wish;

do { found=0; cout<<"enter the number to view:"; cin>>no;"emp.dat",ios::in); *)this,sizeof(employee)); while(!f.eof()) { if(eno==no) { found=1; cout<<"no"<<eno<<endl<<"ename"<<ename<<endl<<"basic:"<< basic<<endl<<"pf"<<pf<<endl<<"hra"<<hra<<endl<<"net"<< net<<endl; } *)this,sizeof(employee)); } if(!found) cout<<"Employee record not found"<<endl; cout<<"Wish to record another employee:[y/n]:"; cin>>wish; } while(wish=='y'); }

void employee::modify_employee() { fstream f; int no,found; char wish; do { found=0;"emp.dat",ios::in); cout<<"enter the employee number to modify:"; cin>>no; *)this,sizeof(employee)); while(!f.eof()) { if(eno==no) { found=1; cout<<"ename:"<<ename<<endl; cout<<"Enter new basic:"; cin>>basic; cout<<"Enter new pf:"; cin>>pf; cout<<"Enter new hra:"; cin>>hra;

net=basic+pf+hra; f.seekp(f.tellg()-sizeof(employee)),ios::beg; f.write((char *)this,sizeof(employee)); } *)this,sizeof(employee)); } if(!found) cout<<"Employee record not found"<<endl; cout<<"Wish to record another employee:[y/n]:"; cin>>wish; } while(wish=='y'); }

void main() { employee e; int choice; do { clrscr(); cout<<"1.Add a new employee record"<<endl; cout<<"2.Delete an existing record"<<endl; cout<<"3.Modify an existing record"<<endl;

cout<<"4.View an existing record"<<endl; cout<<"5.Exit"<<endl; cin>>choice; switch(choice) { case 1: e.new_employee(); break; case 2: e.del_employee(); break; case 3: e.modify_employee(); break; case 4: e.show_employee(); break; default: cout<<"Invalid Choice"; }} while(choice!=5); }

 To write and read the contents in afile we must create an object of class ofstream & ifstream The open() function is used to open different files that uses the same stream object The close() function is used to close a file. It takes no parameters and returns no value The put() function is used for writing one character at a time in a file The get() function is used for reading one charater at a time from a file The write() function is member function of the ofstream class which writes text in a line until it encounters a new line character

1. Write a Program to create Binary File using student

details such as Enrollment Number,Name,Course name , Duration and fees etc.,