Programming

C++ Program to search in a file

Gautam Gautam Donate Nov 27, 2019 · 3 mins read
C++ Program to search in a file

We can perform a search in a binary file opened in input mode by reading each record then checking whether it is our desired record or not. Let's take an example if you want to search for a record for a student of rollno - 10 in file stu.dat,  you can implement this search process in C++ in two manners -

  1. with records implemented through structures
  2. with records implemented through classes

Let's take an example to implement this search option using classes-

Please note the file Stu.dat must exist or must be previously implemented in the C++ Program. 

/*
Program to search in a file having records maintained through classes
*/
#include<fstream.h>
#include<conio.h>
class stu
{
    clrscr();
    int rollno;
    char name[90];
    char Class[4];
    float marks;
    char grade;
    public:
      void getdata()
      {
	  cout<<"Roll No.:";  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<<name<<", Rollno: "<<rollno<<" has "<<marks
	  <<"% marks and"<<grade<<" grade. "<<endl;
      }
      int getrollno()  //accessor function
      {
	  return rollno;
      }
}s1;
void main()
{
    int rn, found = 'n';
    ifstream fi("Stu.dat", ios::in); //this file must exist on disk!
      cout<<"\n Enter rollno of student to search: ";
      cin>>rn;
    while(!fi.eof())
    {
	fi.read((char *)&s1, sizeof(s1));
	if(s1.getrollno()==rn)
	{
	    s1.putdata();
	    found='y';
	    break;
	}
    }
    if(found == 'n')
	cout<<"\n RollNo not found!"<<endl;
    fi.close();
    getch();
}

Output -

Enter rollno of student to search: 10

Nick, rollno 10 has 90% marks and A grade.

Enter rollno of student to search: 30

Rollno not found!

Now, let's implement this program through structures -

/*
Program to implement searching in a file that has records maintained through structure
*/

#include<fstream.h>
struct stu
{
    int rollno;
    char name[90];
    char Class[4];
    float marks;
    char grade;
}s1;
void main()
{
    int rn;
    char found='n';
    ifstream fi ("stu.dat", ios :: in); //file stu.dat must exist on disk
    cout<<"\n Enter rollno to be searched for: ";
    cin>>rn;
    while(!fi.eof())
    {
	fi.read((char *) &s1, sizeof(s1));
	if(s1.rollno ==rn)
	{
	    cout<<s1.name<<", name "<<rn<<" has "<<s1.marks<<"% marks and "
	    <<s1.grade<<" grade."<<endl;
	    found = 'y';
	    break;
	}
    }
    if (found == 'n')
       cout<<"\n Roll number not found! "<<endl;
    fi.close();
}

Output -

Enter rollno of student to search: 10
Nick, rollno 10 has 90% marks and A grade.
Enter rollno of student to search: 30
Rollno not found!

Tags

Join Newsletter
Get the latest news right in your inbox. We never spam!
Gautam
Written by Gautam Sharma Profile
Developer, Writer, Emotional, loves to help, blogger

Connect with Gautam