Real-Life Project in C++ Using File Handling
Last Updated on: 23rd Dec 2025 16:51:13 PM
Project: Student Management System
In real-world applications, programs must store and manage data permanently so that information is not lost when the program stops running. Educational institutes, training centers, and online learning platforms regularly maintain student records such as roll number, name, course, and fees. This data needs to be saved, updated, and retrieved efficiently.
In this tutorial, we will build a Student Management System in C++ using File Handling, which is a practical and industry-relevant project. This project demonstrates how C++ programs interact with files to store student records permanently and perform operations like adding, viewing, and searching student data.
Project Description
The Student Management System is a console-based C++ application that allows users to:
-
Add new student records
-
View all student records
-
Search student by roll number
-
Update existing student details
-
Delete student records
-
Permanent storage using binary files
All student data is stored permanently in a file using file handling, making this project suitable for beginners as well as intermediate learners.
Real-Life Use Case
This type of system is used in coaching institutes, colleges, training centers, and EdTech platforms where student data needs to be stored safely and accessed whenever required.
Tools and Concepts Used
-
C++ Programming Language
-
File Handling (
fstream) -
Classes and Objects
-
Functions
-
Menu-driven program
Student Record Structure
Each student record contains:
-
Roll Number
-
Name
-
Course
-
Fees
Step 1: Define the Student Class
A class is used to group student-related data and functions together. This makes the program organized and easy to manage.
Example (Class Definition)
#include <iostream>
#include <fstream>
using namespace std;
class Student {
public:
int roll;
char name[30];
char course[30];
float fees;
void input() {
cout << "Enter Roll Number: ";
cin >> roll;
cin.ignore();
cout << "Enter Name: ";
cin.getline(name, 30);
cout << "Enter Course: ";
cin.getline(course, 30);
cout << "Enter Fees: ";
cin >> fees;
}
void display() {
cout << "Roll Number: " << roll << endl;
cout << "Name: " << name << endl;
cout << "Course: " << course << endl;
cout << "Fees: " << fees << endl;
cout << "------------------------" << endl;
}
};
Step 2: Add Student Record to File
This function takes student input and writes the complete student object into a file so that the data is stored permanently.
Program
void addStudent() {
Student s;
ofstream file("students.dat", ios::binary | ios::app);
s.input();
file.write((char*)&s, sizeof(s));
file.close();
cout << "Student record added successfully." << endl;
}
Step 3: View All Student Records
This function reads all student records from the file and displays them one by one on the screen.
Program
void viewStudents() {
Student s;
ifstream file("students.dat", ios::binary);
while (file.read((char*)&s, sizeof(s))) {
s.display();
}
file.close();
}
Step 4: Search Student by Roll Number
This function searches for a student record in the file based on roll number and displays the details if found.
Program
void searchStudent() {
Student s;
int rollNo;
bool found = false;
cout << "Enter Roll Number to Search: ";
cin >> rollNo;
ifstream file("students.dat", ios::binary);
while (file.read((char*)&s, sizeof(s))) {
if (s.roll == rollNo) {
s.display();
found = true;
break;
}
}
file.close();
if (!found) {
cout << "Student record not found." << endl;
}
}
Step 5: Main Menu-Driven Program
The main function provides a menu so the user can choose different operations repeatedly until exit.
Complete Program (Final Code)
#include <iostream>
#include <fstream>
using namespace std;
class Student {
public:
int roll;
char name[30];
char course[30];
float fees;
void input() {
cout << "Enter Roll Number: ";
cin >> roll;
cin.ignore();
cout << "Enter Name: ";
cin.getline(name, 30);
cout << "Enter Course: ";
cin.getline(course, 30);
cout << "Enter Fees: ";
cin >> fees;
}
void display() {
cout << "Roll Number: " << roll << endl;
cout << "Name: " << name << endl;
cout << "Course: " << course << endl;
cout << "Fees: " << fees << endl;
cout << "--------------------------" << endl;
}
};
void addStudent() {
Student s;
ofstream file("students.dat", ios::binary | ios::app);
s.input();
file.write((char*)&s, sizeof(s));
file.close();
cout << "Student record added successfully." << endl;
}
void viewStudents() {
Student s;
ifstream file("students.dat", ios::binary);
while (file.read((char*)&s, sizeof(s))) {
s.display();
}
file.close();
}
void searchStudent() {
Student s;
int r;
bool found = false;
cout << "Enter Roll Number to Search: ";
cin >> r;
ifstream file("students.dat", ios::binary);
while (file.read((char*)&s, sizeof(s))) {
if (s.roll == r) {
s.display();
found = true;
break;
}
}
file.close();
if (!found) {
cout << "Student record not found." << endl;
}
}
void updateStudent() {
Student s;
int r;
bool found = false;
cout << "Enter Roll Number to Update: ";
cin >> r;
fstream file("students.dat", ios::binary | ios::in | ios::out);
while (file.read((char*)&s, sizeof(s))) {
if (s.roll == r) {
cout << "Enter new details:" << endl;
s.input();
file.seekp(-sizeof(s), ios::cur);
file.write((char*)&s, sizeof(s));
found = true;
break;
}
}
file.close();
if (found) {
cout << "Student record updated successfully." << endl;
} else {
cout << "Student record not found." << endl;
}
}
void deleteStudent() {
Student s;
int r;
bool found = false;
cout << "Enter Roll Number to Delete: ";
cin >> r;
ifstream file("students.dat", ios::binary);
ofstream temp("temp.dat", ios::binary);
while (file.read((char*)&s, sizeof(s))) {
if (s.roll != r) {
temp.write((char*)&s, sizeof(s));
} else {
found = true;
}
}
file.close();
temp.close();
remove("students.dat");
rename("temp.dat", "students.dat");
if (found) {
cout << "Student record deleted successfully." << endl;
} else {
cout << "Student record not found." << endl;
}
}
int main() {
int choice;
do {
cout << "\n1. Add Student";
cout << "\n2. View Students";
cout << "\n3. Search Student";
cout << "\n4. Update Student";
cout << "\n5. Delete Student";
cout << "\n6. Exit";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: addStudent(); break;
case 2: viewStudents(); break;
case 3: searchStudent(); break;
case 4: updateStudent(); break;
case 5: deleteStudent(); break;
case 6: cout << "Exiting program."; break;
default: cout << "Invalid choice.";
}
} while (choice != 6);
return 0;
}
Sample Output
1. Add Student
2. View Students
3. Search Student
4. Update Student
5. Delete Student
6. Exit
Enter your choice: 4
Enter Roll Number to Update: 101
Enter new details:
Student record updated successfully.
Explanation of Output
The program displays a menu that allows the user to select different operations. When updating, the program searches for the roll number and overwrites the existing record. When deleting, the program copies all records except the selected one into a temporary file and replaces the original file.
Advantages of This Project
This project provides permanent data storage, supports real-world operations like update and delete, strengthens file handling concepts, and improves logical thinking.
Limitations of This Project
The program uses a console interface, does not include authentication, and stores data without encryption.
Conclusion
This enhanced Student Management System demonstrates how real-world applications manage data using file handling in C++. By implementing add, search, update, and delete operations, students gain a strong understanding of how CRUD operations work internally. This project is an excellent foundation for learning database systems and backend development concepts.
Keep practicing — you're doing amazing!
Happy Coding! ![]()