0% found this document useful (0 votes)
86 views17 pages

Program To Search in A File Having Record Maintained Through Classes

The document contains programs written in C++ that perform various operations on student records stored in files, including: 1) Searching for a student record by roll number in a file containing records as classes. 2) Reading and writing class objects to and from a file. 3) Searching for a student record by roll number in a file containing records as structures. 4) Appending new student records to an existing file, modifying records in a file, deleting a record from a file, and summarizing the contents of a file after changes.

Uploaded by

udit kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views17 pages

Program To Search in A File Having Record Maintained Through Classes

The document contains programs written in C++ that perform various operations on student records stored in files, including: 1) Searching for a student record by roll number in a file containing records as classes. 2) Reading and writing class objects to and from a file. 3) Searching for a student record by roll number in a file containing records as structures. 4) Appending new student records to an existing file, modifying records in a file, deleting a record from a file, and summarizing the contents of a file after changes.

Uploaded by

udit kashyap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

-: Program to search in a file having record

maintained through classes:-


#include<iostream.h>
#include<fstream.h>
class stu
{
char name[25];
char Class[4];
float marks:
char grade;
public:
void getdata()
{
cout<<"Rollno :";
cin>>rollno;
cout<<"Name :";
cin>>name;
cout<<"Class:";
cin>>Class;
cout<<"Marks :";
cin>>marks;
if(marks>=75)
grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(maarks>=40) grade='D';
else grade='F';
}
void putdata()
{
cout<<name<<",roolno"<<rollno<<""has"<<marks<<
"%marks and"<<grade<<"grade."<<endl;
}
int getrno()
{
return rollno;
}
}s1;
int main()
{
int rn; char found='n';
ifstraem fi("stu.dta",ios::in);
cout<<"Enter rollno to be searched for :";
cin>>rn;
while(!fi.eof())
{
fi.read((char*)&s1,sizeof(s1));

2
-:Program for reading and writing class
objects:-
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
class Student
{
char name[40];
char grade;
float marks;
public:
void getdata(void);
void display(void);
};
void Student::getdata(void)
{
char ch;
cin.get(ch);
cout<<"Enter name:"; cin.getline(name,40);
cout<<"Enter grade:"; cin>>grade;
cout<<"Enter marks:"; cin>>marks;
cout<<"\n";
}
void Student::display(void)
{
cout<<"Name:"<<name<<"\t"<<"Grade:"<<grade<<"\t"<<"Mark
s:"<<marks<<"\t"<<"\n";
}
int main()
{
system("cls");
Student arts[3];
fstream filin;
3
filin.open("Stu.dta",ios::in|ios::out);
if(!filin)
{
cout<<"Cannot open file!!|n";
return 1;
}
cout<<"Enter details for 3 students |n";
for(int i=0;i<3;i++)
{
arts[i].getdata();
filin.write((char*)&arts[i],sizeof(arts[i]));
}
filin.seekg(0);
cout<<"The content of stu.dat are shown below.|n";
for(i=0;i<3;i++)
{
filin.read((char*)&arts[i],sizeof(arts[i]));
arts[i].display();
}
filin.close();
return 0;
}
if(s1.getrno()==rn)
{
s1.putdata();
found='y';
break;
}
}
if(found =='n')
cout<<"Rollno not found in file!!"<<endl;
fi.close();
return 0;
}

4
5
-:Program to implement searching in a file
that has records maintained through
structures:-
#include<iostream.h>
#include<fstream.h>
struct stu
{
int rollno;
char name[25];
char Class[4];
char grade;
float marks;
}s1;
int main()
{
int rn ; char found ='n';
ifstream fin("stu.dat",ios::in);
cout<<"Enter rollno to be searched for:";
cin>>rn;
while(!fin.eof())
{
fin.read((char*)&s1,sizeof(s1));
if(s1.rollno==rn)
{
cout<<s1.name<<",rollno"<<rn<<"has"<<s1.marks<<"%marks
and"<<s1.grade<<"grade."<<endl;
found='y';
break;
}
}
if(found=='n')
cout<<"Rollno not found in file!!"<<endl;
fin.close();
return 0;}
6
7
-:Program to append data in a file:-
#include<iostream.h>
#include<fstream.h>
class stu
{
int rollno;
char name[25];
char Class[4];
char grade;
float marks;
public:
void getdata()
{
cout<<"Rollno : "; cin>>rollno;
cout<<"Name :";cin>>name;
cout<<"Class :";cin>>Class;
cout<<"Marks :";cin>>marks;
if(marks>=75) grade = 'A';
else if(marks>=60) grade='B';
else if (marks>=50) grade='C';
else if(marks>=40) grade='D';
}
void putdata()
{
cout<<name<<",rollno"<<rollno<<"has"<<marks
<<"%maks and"<<grade<<"grade"<<endl;
}
int getrno()
{
return rollno;
}
} s1;
int main()
{
8
ofstream fo("stu.dat",ios::app|ios::binary);
char ans='y';
while(ans =='y')
{
s1.getdata();
fo.write((char*)&s1,sizeof(s1));
cout<<"Record added to file.\n";
cout<<"Want to enter more records ?(y/n)..";
cin>>ans;
}
fo.close();
return 0;
}

9
-:Program to delete a record from a file:-

#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
class stu
{
int rollno;
char name[25];
char Class[4];
float marks;
char grade;
public
void getdata()
{
cout<<"Rollno :";
cin >> rollno;
cout<<"Name :";
cin >> name;
cout<<"Class :";
cin >> Class;
cout<<"Marks: ";
cin >> marks;
if(marks >+75) grade = 'A';
else if(marks>=60) grade = 'B';
else if(marks>=50) grade = 'C';
else if(marks>=40) grade = 'D';
else grade = 'F';
}
void putdata()
{
cout<<"Rollno"<<rollno<<"\t
Name:"<<name<<"\nMarks :"<<marks<<"\t
Grade :"<<grade<<endl;
}
int getrno()
10
{ return rollno ; }
} s1,stud ;
int main()
{
ifstream fio("stu.dat",ios::in);
ofstream file("temp.dat",ios::out);
int rno; char found =' f', confirm = 'n';
cout<<"Enter rollno of student whose record
is to be deleted \n";
cin>>rno;
while(!fio.eof())
{
fio.read((char*)&s1,sizeof(s1));
if(1s.getrno()==rno)
{
s1.putdata();
found = 't';
cout<<"Are you sure,you want to delete
this record ?(y/n)..";
cin>>confirm:
if(confirm = = 'n')
file.writte((char*)&s1,sizeof(s1));
}
else
file.write((char*)&s1,sizeof(s1));
}
if(found == 'f')
cout<<"Record not found!! \n";
fio.close();
file.close();
remove("stu.dat","stu.dat");
rename("temp.dat","stu.dat");
fio.opne("stu.dat",ios::i );
cout<<"Now the file contains\n";
while(!fio.eof())
{
fio.read((char*)&stud,sizeof(stud));

11
if(fio.eof())break;
stud.putdata();
}
fio.close();
return 0 ;
}

12
-:Program to modify data in a given file:-
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
Class str
{
int roll;
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{
cout<<”Rollno:”;
cin>>rollno;
cout<<”Name:”;
cin>>name;
cout<<”Class:”;
cin>>Class;
cout<<” Marks:”;
cin>> marks;
if(marks >=75)
grade = ‘A’;
else if( marks >= 60)
grade = ’B’;
else if( marks >= 50)
grade = ’C’;
else if( marks >= 40)
grade = ’D’;
else grade =’F’;
}
Void putdata()

13
{
cout<<” Rollno” << rollno <<” \t Name : “<<name << “ \n
Marks : “<< marks <<” \t Grade :
“<<grade<< endl ;

}
int getno()
{
Return rollno;
}
void modify() ;
}s1,stud ;
void stu::modify()
{
cout<< “ Rollno : “<<rollno <<endl ;
cout<<” Name : “<<name <<”\t Class : “<<class
<<”\t Marks : << marks <<endl;
cout<< “ Enter new details ” << endl ;
char nm[20]= “ “,CI[4]= “ “;
float mks;
cout<<” New Name :(Enter’.’to retain old
one) ” ;
cin>>” nm ;
cout<<” New Class :(press’.’to retain old one)”;
cin>>Cl ;
cout<<“ New Marks :(press -1 to retain old
one)” ;
cin>> mks ;
if( strcmp ( nm ,”.” ) ! = 0 )
srtcpy ( name , nm ) ;
if ( strcmp ( Cl , “ . “ ) ! = 0)
strcpy ( Class , Cl ) ;
{
marks = mks;
if ( marks > = 75 ) grade =’A’ ;
else if ( marks > = 60 ) grade =’B’ ;
else if ( marks > = 50 ) grade =’C’ ;

14
else if ( marks > = 40 ) grade =’D’ ;
else grade = ‘F’ ;
}
}
int main()
{
fstream fio( “ stu.dat “ , ios::in|ios::out|ios::binary);
int rno ; long pos; char found = ‘ f ’;
cout << “ Enter rollno of student whose record is to
be modified \n “ ;
cin >> rno ;
while( !fio.eof () )
{
pos = fio.tellg() ;
fio.read( (char*) & s1,sizeof (s1) ) ;
if ( s1.getrno () = = rno)
{
● s1.modify();
fio.seekg(pos) ;
fio.write( ( char*) &, sizeof (s1) );
found = ‘t’ ;
break ;
}
}
If( found = = ‘ f ’ )
cout<< “ Record not found!!\n” ;
fio.seekg (0) ;
cout<< “ Now the file contains\n “ ;
while(!fio.eof () )
{
fio.read( (char*) & stud, sizeof(stud) ) ;
stud.putdata();
}

fio.close () ;
return 0 ;
}

15
16
17

You might also like