πŸŽ“ 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

  1. Open any C compiler (GCC / CodeBlocks / Turbo C)

  2. Copy and paste the code

  3. Compile the program

  4. Run the executable

  5. 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:

  1. Open the original file (student.txt) in read mode
  2. Create a temporary file (temp.txt)
  3. Copy all records except the one to be deleted
  4. Delete the original file
  5. 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

Popular posts from this blog

🌟 The Honest Journey of a Student: Learning, Failing, and Growing

“C Programming for Beginners: Master Variables, Data Types, and Memory (Bits Explained)”