In this post we will write a C++ program to open a file in binary mode & then get data in that file. user will enter the name of file and program will print the data of that file.
C++ program to open a file in binary mode & then get data in that file.
#include<iostream>
#include<conio.h>
#include<fstream>
#include<process.h>
using namespace std;
class rec
{
private:
int roll;
int long phon;
char name[100];
public:
dis()
{
std::cout<<name<<"t"<<roll<<"t"<<phon<<std::endl;
}
};
int main()
{
system("cls");
char na[100];
rec r;
std::cout<<"Enter the file name = ";
std::cin>>na;
ifstream sec(na,ios::binary);
if(!sec)
{
std::cout<<"File opening error"<<std::endl;
getch();
exit(0);
}
sec.read((char *)&r, sizeof(r));
while(sec)
{
r.dis();
sec.read((char *)&r, sizeof(r));
}
getch();
return 0;
}
Output
Enter the file name = temp.bin ABC 1 12345 XYZ 2 54321