This is the code:
Code:
/*Q 2. Assuming the class Applicant given below, write functions in
C++ to perform the following :
class Applicant
{
char A_Rno[10];
char A_Name[20];
int A_Score;
};
(i) Write the objects of class to a binary file.
(ii) Reads the objects of class from binary file and
display them on screen.
(iii) Searches for a particular A_Rno.
(iv) Reads the file and lets the user make changes in data and
invokes the function to display the values.
(v) Just displays contents of the file.
Note : Include all the required function’s prototypes in the
public section of the class.*/
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<string.h>
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<dos.h>
int count=0;
class applicant
{
char A_Rno[10];
char A_Name[20];
int A_Score;
public:
void getdata();
void showdata();
void search();
void mod();
}app;
void applicant::getdata()
{
ofstream fout("app.dat",ios::binary);
cout<<"\nEnter Rno :";
cin.getline(A_Rno,10);
cout<<"\nEnter Name :";
cin.getline(A_Name,20);
fout<<A_Rno<<A_Name;
fout.close();
count++;
}
void applicant::showdata()
{
ifstream fin("app.dat",ios::binary);
for(int i=0;i<count;i++)
{
fin.getline(A_Rno,10);
fin.getline(A_Name,20);
cout<<"\nRno : "; puts(A_Rno);
cout<<"\nName : "; puts(A_Name);
}
fin.close();
}
void applicant::search()
{
char rno[10];
cout<<"\nEnter the applicant's Rno to be searched.. ";
cin.getline(rno,10);
ifstream fin("app.dat",ios::binary);
for(int i=0;i<count;i++)
{
fin.getline(A_Rno,10);
fin.getline(A_Name,20);
if(strcmp(A_Rno,rno))
{
cout<<"\nApplicant found...\n";
cout<<"\nRno : "; puts(A_Rno);
cout<<"\nName : "; puts(A_Name);
}
}
fin.close();
}
void applicant::mod()
{
int pos; char rno[10];
cout<<"\nEnter the applicant's Rno to be modified.. ";
cin.getline(rno,10);
fstream fin("app.dat",ios::binary);
for(int i=0;i<count;i++)
{
fin.getline(A_Rno,10);
fin.getline(A_Name,20);
if(strcmp(A_Rno,rno))
pos=i;
}
int size=pos*sizeof(applicant);
seekg(size,ios::beg);
cout<<"\nEnter the new data";
cout<<"\nEnter Rno :";
cin.getline(A_Rno,10);
cout<<"\nEnter Name :";
cin.getline(A_Name,20);
fin<<A_Rno<<A_Name;
fin.close();
}
void main()
{
clrscr();
/*nt mrec=0,offset=0;
fstream finout("student.dat",ios::binary);
if(!student)
{
cout<<"\nFile could not be opened...";
getch();
exit(0);
} */
begin:
clrscr();
cout<<" MENU\n\n"
<<" 1.Enter student data\n"
<<" 2.Display Student data\n"
<<" 3.Search for a record\n"
<<" 4.Modify a record\n"
<<" 5.Exit\n\n"
<<" Enter your choice... ";
int ch; cin>>ch;
if(ch==1)
{
app.getdata();
/*mrec=count;
offset=((mrec-1)*sizeof(applicant));
finout.seekp(offset,ios::beg);
finout.write((char *)&app,sizeof(applicant));*/
goto begin;
}
if(ch==2)
{
app.showdata(); goto begin;
}
if(ch==3)
{
app.search(); goto begin;
}
if(ch==4)
{
app.mod(); goto begin;
}
exit(0);
}