In this post we will write a C++ program to search a record from binary file.
C++ program to search a record from binary file.
#include<iostream>
#include<conio.h>
#include<process.h>
#include<fstream>
using namespace std;
class rec
{
protected:
int roll;
int long phon;
char name[100];
public:
dis()
{
std::cout<<name<<"t"<<roll<<"t"<<phon<<std::endl;
}
};
int main()
{
int n;
rec r;
system("cls");
ifstream first("new.cpp",ios::binary|ios::in);
if(!first)
{
std::cout<<"File opening error"<<std::endl;
getch();
exit(0);
}
first.seekg(0);
std::cout<<"Enter the number of record = ";
std::cin>>n;
long int pos = (n-1)*sizeof(r);
first.seekg(pos);
first.read((char *)&r, sizeof(r));
r.dis();
getch();
return 0;
}
Output
Enter rthe number of records = 2 XYZ 2 54321