Read Student Record File – Write a C++ program to read and display all records of the student file.
Source Code
#include <iostream> #include <fstream> using namespace std; // Define the structure struct Student { int rno; char name[30]; int marks; }; int main() { Student srec; // Open the binary file for reading fstream file1("d://file.dat", ios::binary|ios::in); if (!file1) { cout << "Error in opening file." << endl; return 1; } // Read and display all records from the file Student srecord; while (file1.read((char*)&srec, sizeof(srec))) { cout << "Roll Number: " << srec.rno << endl; cout << "Name: " << srec.name << endl; cout << "Marks: " << srec.marks << endl; cout << endl; } // Close the file file1.close(); return 0; }
Output
Roll Number: 1
Name: ahmad
Marks: 900
Roll Number: 2
Name: yasir
Marks: 800
Roll Number: 3
Name: javed
Marks: 901
Roll Number: 4
Name: kamran
Marks: 805
Roll Number: 5
Name: irshad
Marks: 709
Roll Number: 6
Name: wahid
Marks: 840
——————————–
Process exited after 0.1375 seconds with return value 0
Press any key to continue . . .