Vous êtes sur la page 1sur 3

//A program to illustrate Binary file Handling 2 if(!

x)
cout<<"\nNo File was found!!";
#include<fstream.h> while(x.read((char*)&ob,sizeof(ob)))
#include<conio.h>
#include<process.h> cout<<"\n\nCode:"<<ob.ecode<<"\nName"<<o
#include<stdio.h> b.ename<<"\nDesignation:"<<ob.desig<<"\nSal
#include<math.h> ary:"<<ob.sal;
#include<string.h> x.close();
#include<stdlib.h> break;
struct emp case 3:float scode; //Modify function
{ fstream
int ecode; tx1("tfi|e.txt",ios::binary|ios::in|ios::out|ios::ap
char ename[50]; p);
char desig[50]; x.open("file.txt",ios::binary|ios::in|ios::out);
float sal; cout<<"\nEnter the code of the record to be
}ob,tob; modified:";
void main() cin>>scode;
{ while(x.read((char*)&ob,sizeof(ob)))
clrscr(); {
int opt; if(scode==ob.ecode)
char ch; {
fstream x; char modyn;
do cout<<"\nNew record:";
{ cout<<"\nEnter code:";
clrscr(); cin>>tob.ecode;
cout<<"\nEnter name:";
cout<<"\n***MENU***\n\n1.WRITE\n2.READ\ gets(tob.ename);
n3.MODIFY\n4.DELETE\n\n"; cout<<"\nEnter designation:";
cout<<"Enter Option:"; gets(tob.desig);
cin>>opt; cout<<"\nEnter salary:";
switch(opt) cin>>ob.sal;
{ cout<<"\n\nDo you want to replace
case this:\n\nCode:"<<ob.ecode<<"\nName:"<<ob.e
1:x.open("file.txt",ios::app|ios::out|ios::binary); name<<"\nDesignation:"<<ob.desig<<"\nSalary:
//writing into file "<<ob.sal<<"\n\n with
cout<<"\nEnter code:"; this:\n\nCode:"<<tob.ecode<<"\nName:"<<tob.
cin>>ob.ecode; ename<<"\nDesignation:"<<tob.desig<<"\nSala
cout<<"\nEnter name:"; ry:"<<tob.sal;
gets(ob.ename); cout<<"\n\n(y/n):";
cout<<"\nEnter designation:"; cin>>modyn;
gets(ob.desig); if(modyn=='y')
cout<<"\nEnter salary:"; tx1.write((char*)&tob,sizeof(tob));
cin>>ob.sal; else
x.write((char*)&ob,sizeof(ob)); tx1.write((char*)&ob,sizeof(ob));
x.close(); }
break; else
case 2:x.open("file.txt",ios::binary|ios::in); {
//reading from file cout<<"\nCode does not exist!\n";
tx1.write((char*)&ob,sizeof(ob)); OUTPUT
}
} ***MENU***
remove("file.txt"); 1.WRITE
rename("tfile.txt","file.txt"); 2.READ
break; 3.MODIFY
case 4: fstream 4.DELETE
tx2("tfile.txt",ios::binary|ios::in|ios::out|ios::ap
p); //Delete function Enter Option:1
x.open("file.txt",ios::binary|ios::in|ios:: Enter code:10
out); Enter name: Karun
cout<<"\nEnter the code of the record Enter designation: Ceo
to be deleted:"; Enter salary:15000
cin>>scode; Do you want to continue??(y/n):y
char delyn;
while(x.read((char*)&ob,sizeof(ob))) ***MENU***
if(scode==ob.ecode) 1.WRITE
{ 2.READ
cout<<"\n\nFound 3.MODIFY
Record:\n\nCode:"<<ob.ecode<<"\nName:"<<o 4.DELETE
b.ename<<"\nDesignation"<<ob.desig<<"\nSala
ry"<<ob.sal; Enter Option:2
cout<<"\nDo you want to delete this
record ?(y/n):"; Code: 10
cin>>delyn; Name: Karun
if(delyn=='y') Designation: Ceo
tx2.write((char*)&tob,sizeof(tob)); Salary: 15000
} Do you want to continue??(y/n):y
else
{ ***MENU***
tx2.write((char*)&ob,sizeof(ob)); 1.WRITE
} 2.READ
remove("file.txt"); 3.MODIFY
rename("tfile.txt","file.txt"); 4.DELETE
break;
default:cout<<"\nWrong Option"; Enter Option:3
} Enter the code of the record to be modified:10
cout<<"\nDo you want to continue??(y/n):";
cin>>ch; New record:
} Enter code:12
while(ch=='y'); Enter name:vishal
getch(); Enter designation:Manager
} Enter salary:12000

Do you want to replace this:

Code:10
Name: Karun
Designation: Ceo
Salary:12000

with this:

Code:12
Name:vishal
Designation:Manager
Salary:0

(y/n):y

Do you want to continue??(y/n):y

***MENU***

1.WRITE
2.READ
3.MODIFY
4.DELETE

Enter Option:4

Enter the code of the record to be deleted:12

Do you want to continue??(y/n):n

Vous aimerez peut-être aussi