C++ Program – Binary File Handling

by Nideesh C on May 2, 2011 · 0 comments

in C++




//Downloaded From theonlinetutorials.com
//A program to illustrate Binary file Handling 

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



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ class program – illestrate File operations
  2. C++ Program – Using FileHandling..Progress Report
  3. C++ Program – Supermarket billing
  4. C++ Program – Convert decimal to binary & binary to decimal
  5. C++ Program – Convert Binary to Octal & Octal to Binary

Leave a Comment

Previous post:

Next post: