Basic Operation on Binary File

Basic Operation On Binary File In C++
Considering the following class declaration, write functions to create and read a binary file:
class student
{
int rollno;
char name[20];
float marks;
public:
void getdata()
{
cout<<“\nEnter The rollno. “;
cin>>rollno;
cout<<“\n\nEnter The Name of The Student “;
gets(name);
cout<<“\nEnter The marks. “;
cin>>marks;
}
void showdata()
{
cout<<“\nRoll no. : “<<rollno;
cout<<“\nStudent Name : “<<name;
cout<<“\n The marks are :”<<marks;
}
int retrollno()
{ return rollno;
}
float retmarks()
{return marks;}
char * retname()
{return name;}
};

Function to create a binary file with n records

void write_data(int n)
{
student s;
ofstream fout;
fout.open(“student.dat”,ios::binary|ios::app);
          int i=0;
         while(i<n)
{
s.getdata();
fout.write((char*)&s, sizeof(s));
i++;
}
fout.close();
}

function to read a binary file and display the records

void display()
{
student s;
ifstream fin;
fin.open(“student.dat”,ios::binary);
while(fin.read((char*)&s, sizeof(s)))
{
s.showdata();
}
}
fin.close();
}

Function to search for a given rollno and display the record from binary file

void search (int n)
{
student s;
ifstream fin;
fin.open(“student.dat”,ios::binary);
while(fin.read((char*)&s,sizeof(s)))
{
if(s.retrollno()==n)
s.showdata();
}
fin.close();
}

Function to search for a given name and display the record from binary file

void search (char inputstring)
{
student s;
ifstream fin;
fin.open(“student.dat”,ios::binary);
while(fin.read((char*)&s,sizeof(s)))
{
if(strcmpi(s.retname(),inputstring)==0)
s.showdata();
}
fin.close();
}

Function to count the no of students scoring above 90 from binary file

void count_topscorers ()
{
student s;
ifstream fin;

 

int count=0;
fin.open(“student.dat”,ios::binary);
while(fin.read((char*)&s,sizeof(s)))
{
if(s.retmarks()>90)
count++;          }

 

cout<<”No of students coring above 90 are: <<count;
fin.close();
}
Function to delete a record based on a condition

Delete the record of student having a given rollno (passed as an argument)

void deleterecord(int n)
{
student s;
ifstream fin;
fin.open(“student.dat”,ios::binary);
ofstream fout;
fout.open(“Temp.dat”,ios::out|ios::binary);
while(fin.read((char*)&s,sizeof(s)))
{
if(s.retrollno!=n)
                                  fout.write((char*)&s,sizeof(s));
}
fin.close();
fout.close();
remove(“student.dat”);
rename(“Temp.dat”,”student.dat”);
}

Function to update  a record based on a condition

void updaterecord(int n)
{
fstream file;
student s;
int found=0, records=0;
file.open(“student.dat”,ios::in|ios::out);
while(!found && file.read((char*)&s,sizeof(s))
{       records++;
if(s.retrollno()==n)
{
cout<<”\n Displaying details of student:\n”;
                              s.showdata();
cout<<“\nEnter The New Details of student”;
s.getdata();
                             file.seekp((records-1) * sizeof(s),ios::cur);
file.write((char*)&s,sizeof(s));
found=1;
}
}
file.close();
}

Author: rakesh kumar

cbsetoday.com is my online home, here I share all the information related to education, programming - and everything else that attract me. You can get in touch with me in these social sharing websites

Leave a Reply