C ++ Program To Display All File Records

By | July 7, 2019

Task: Write a (C++) C Plus Plus Program To Read and Display All Records from a Binary File.

The source code of C Plus Plus Program To Read and Display All Records from a Binary File

/* Write a C Program to read and display 
all records from an existing binary file
*/
#include<iostream>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
#include<string>
using namespace std;

struct student
{
int rollno;
char name[30];
}srecord;

fstream file1;
int main()
{
char ans;
int i;
//opening binary file in reading mode	

file1.open("d://cppfile.dat",ios::binary |ios::in);

if(!file1)
{
cout<<"File could not open";
exit(0);
}

while(!file1.eof())
{
file1.read((char*)&srecord,sizeof(srecord));
if(!file1.eof())
{
cout<<"\n------------------\n";
cout<<"Roll No:"<<srecord.rollno;
cout<<"\nName:"<<srecord.name;
}
}
file1.close();
cout<<"\nProgram ended: C++ Program To Read and Display all Student Records from Binary File:";
return 0;
}

A sample run of the C++ Read Binary File Program

------------------
Roll No:1
Name:sajid
------------------
Roll No:2
Name:majid
Program ended: C++ Program To Read and Display all Student Records from Binary File:
--------------------------------
Process exited after 3.887 seconds with return value 0
Press any key to continue . . .

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *