π Student Management System in C (With File Handling) – Complete Project
π· Introduction
In this project, we will build a Student Management System in C Programming that helps manage student records efficiently.
This system allows users to add, view, search, and delete student records using file handling.
π This is a real-world project and perfect for students to understand how C is used practically.
π· π Project Preview
Menu-driven system
Stores student data in file
Easy-to-use interface
Real-time add, search, delete operations
π· π Project Flow (Simple Diagram)
User
↓
Menu
↓
Function
↓
File Handling
↓
Output
π· ✨ Features of the Project
➕ Add Student Record
π View All Students
π Search Student by ID
❌ Delete Student Record
πΎ Permanent storage using file handling
π‘Real-World Use
This type of Student Management System is used in many real-life applications:
- Used in schools and colleges to store and manage student records such as ID, name, and marks
- Helps administrators and teachers easily add, search, and update student information
- Acts as a basic version of a database management system
- Used in training institutes and coaching centers to maintain student data
- Reduces manual paperwork and improves data organization and accuracy
- Can be extended into larger systems like college management software
- Forms the base for ERP (Enterprise Resource Planning) systems used in educational institutions
π· π§ Concepts Used
Structures
File Handling
Functions
Arrays
Basic Input/Output
π· π» Program Code (With Comments)
#include <stdio.h> // Standard input-output functions (printf, scanf)
#include <stdlib.h> // Standard library (exit function)
#include <string.h> // String handling functions
// Structure to store student data
struct student {
int id; // Student ID
char name[50]; // Student Name
float marks; // Student Marks
};
// Function to add student
void addStudent() {
FILE *fp = fopen("student.txt", "a"); // Open file in append mode
struct student s; // Create student structure variable
printf("Enter ID: "); // Ask user for ID
scanf("%d", &s.id); // Read ID input
printf("Enter Name: "); // Ask user for name
scanf(" %[^\n]", s.name); // Read full name (with spaces)
printf("Enter Marks: "); // Ask user for marks
scanf("%f", &s.marks); // Read marks
fwrite(&s, sizeof(s), 1, fp); // Write student data to file
fclose(fp); // Close file
printf("✅ Student added successfully!\n"); // Success message
}
// Function to view students
void viewStudents() {
FILE *fp = fopen("student.txt", "r"); // Open file in read mode
struct student s; // Create student variable
printf("\n--- Student Records ---\n"); // Display header
while(fread(&s, sizeof(s), 1, fp)) { // Read records one by one
printf("ID: %d | Name: %s | Marks: %.2f\n", s.id, s.name, s.marks); // Print data
}
fclose(fp); // Close file
}
// Function to search student
void searchStudent() {
FILE *fp = fopen("student.txt", "r"); // Open file in read mode
struct student s; // Create student variable
int id, found = 0; // Store search ID and flag
printf("Enter ID to search: "); // Ask user for ID
scanf("%d", &id); // Read ID
while(fread(&s, sizeof(s), 1, fp)) { // Loop through file
if(s.id == id) { // Check if ID matches
printf("Found → ID: %d | Name: %s | Marks: %.2f\n", s.id, s.name, s.marks); // Display result
found = 1; // Mark as found
}
}
if(!found) { // If not found
printf("❌ Student not found!\n"); // Show message
}
fclose(fp); // Close file
}
// Function to delete student
void deleteStudent() {
FILE *fp = fopen("student.txt", "r"); // Open original file
FILE *temp = fopen("temp.txt", "w"); // Open temporary file
struct student s; // Create student variable
int id, found = 0; // Store ID and flag
printf("Enter ID to delete: "); // Ask user for ID
scanf("%d", &id); // Read ID
while(fread(&s, sizeof(s), 1, fp)) { // Read each record
if(s.id != id) { // If ID does NOT match
fwrite(&s, sizeof(s), 1, temp); // Copy to temp file
} else {
found = 1; // Mark record as found
}
}
fclose(fp); // Close original file
fclose(temp); // Close temp file
remove("student.txt"); // Delete original file
rename("temp.txt", "student.txt"); // Rename temp to original
if(found) { // If record was found
printf("✅ Student deleted successfully!\n"); // Success message
} else {
printf("❌ Student not found!\n"); // Failure message
}
}
// Main function
int main() {
int choice; // Variable to store user choice
while(1) { // Infinite loop for menu
printf("\n===== Student Management System =====\n"); // Title
printf("1. Add Student\n"); // Option 1
printf("2. View Students\n"); // Option 2
printf("3. Search Student\n"); // Option 3
printf("4. Delete Student\n"); // Option 4
printf("5. Exit\n"); // Option 5
printf("Enter your choice: "); // Ask user for choice
scanf("%d", &choice); // Read choice
switch(choice) { // Switch case for menu
case 1: addStudent(); break; // Call add function
case 2: viewStudents(); break; // Call view function
case 3: searchStudent(); break; // Call search function
case 4: deleteStudent(); break; // Call delete function
case 5: exit(0); // Exit program
default: printf("❌ Invalid choice!\n"); // Invalid input
}
}
return 0; // End program
}
π· ▶️ How to Run the Program
Open any C compiler (GCC / CodeBlocks / Turbo C)
Copy and paste the code
Compile the program
Run the executable
Follow the menu options
π· π File Storage Explanation
Data is stored in student.txt
Uses binary format (fwrite & fread)
Data remains saved even after closing the program
π· π₯ Sample Output
===== Student Management System =====
1. Add Student
2. View Students
3. Search Student
4. Delete Student
5. Exit
Enter your choice: 1
Enter ID: 101
Enter Name: Krishna
Enter Marks: 89.5
✅ Student added successfully!
π· π Explanation
A structure is used to store student details
File handling allows permanent data storage
The program uses a menu-driven approach
Functions make code clean and reusable
π· π Future Improvements
You can enhance this project by adding:
✏️ Update student record
π Sorting by marks
π Login system
π₯ GUI version
π· π Related Topics
π Learn more:
Structures in C
File Handling in C
Pointers in C
π· ✅ Advantages
Beginner-friendly
Real-world application
Improves logic building
Useful for exams & interviews
π· Deep Explanation (How This Project Works Internally)
π File Handling Logic
In this project, file handling is used to store student data permanently.
The file used is student.txt.
Data is written using fwrite() and read using fread().
Each student record is stored in binary format, which makes the program faster and efficient.
Even after closing the program, the data remains saved in the file.
❌ Delete Operation Logic (Using Temporary File)
Deleting data directly from a file is not possible, so we use a safe method:
- Open the original file (student.txt) in read mode
- Create a temporary file (temp.txt)
- Copy all records except the one to be deleted
- Delete the original file
- Rename temp.txt to student.txt
This ensures safe and correct deletion of records without data corruption.
π· π Conclusion
This project helps you understand how C programming works in real-life applications.
By modifying and improving it, you can build even more powerful systems.
⭐ Important Note:
This project is very important for exams, practicals, and interviews.
Many colleges and interviewers ask similar logic-based programs.
⭐ Tip: Try adding new features and make your own advanced version!
π Keep Learning. Keep Coding. Keep Growing.
✨ Written by Krishna Popat
π± Founder, Learning Growth Hub
Comments
Post a Comment