Student Record Management System in C with Source Code

Student Record Management in C

This mini project helps you manage student records using structures and file handling in C. You can add, search, delete, and display student records.

यह प्रोजेक्ट C language में file handling और structures की मदद से student records को manage करता है। आप records को add, search, delete और display कर सकते हैं।

Source Code


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student {
    int roll;
    char name[100];
    float marks;
};

void addStudent() {
    FILE *fp = fopen("students.dat", "ab");
    struct student s;
    printf("Enter Roll Number: ");
    scanf("%d", &s.roll);
    printf("Enter Name: ");
    scanf(" %[^\n]", s.name);
    printf("Enter Marks: ");
    scanf("%f", &s.marks);
    fwrite(&s, sizeof(s), 1, fp);
    fclose(fp);
    printf("Student added successfully!\n");
}

void displayStudents() {
    FILE *fp = fopen("students.dat", "rb");
    struct student s;
    printf("\n-- Student Records --\n");
    while (fread(&s, sizeof(s), 1, fp)) {
        printf("Roll: %d\nName: %s\nMarks: %.2f\n\n", s.roll, s.name, s.marks);
    }
    fclose(fp);
}

void searchStudent() {
    FILE *fp = fopen("students.dat", "rb");
    struct student s;
    int roll, found = 0;
    printf("Enter roll number to search: ");
    scanf("%d", &roll);
    while (fread(&s, sizeof(s), 1, fp)) {
        if (s.roll == roll) {
            printf("\nRecord Found:\nRoll: %d\nName: %s\nMarks: %.2f\n", s.roll, s.name, s.marks);
            found = 1;
            break;
        }
    }
    if (!found) printf("Record not found.\n");
    fclose(fp);
}

void deleteStudent() {
    FILE *fp = fopen("students.dat", "rb");
    FILE *temp = fopen("temp.dat", "wb");
    struct student s;
    int roll, found = 0;
    printf("Enter roll number to delete: ");
    scanf("%d", &roll);
    while (fread(&s, sizeof(s), 1, fp)) {
        if (s.roll != roll)
            fwrite(&s, sizeof(s), 1, temp);
        else
            found = 1;
    }
    fclose(fp);
    fclose(temp);
    remove("students.dat");
    rename("temp.dat", "students.dat");
    if (found) printf("Student record deleted.\n");
    else printf("Record not found.\n");
}

int main() {
    int choice;
    do {
        printf("\n1. Add Student\n2. Display Students\n3. Search Student\n4. Delete Student\n5. Exit\nEnter choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1: addStudent(); break;
            case 2: displayStudents(); break;
            case 3: searchStudent(); break;
            case 4: deleteStudent(); break;
            case 5: printf("Exiting...\n"); break;
            default: printf("Invalid choice.\n");
        }
    } while (choice != 5);
    return 0;
}
        

Code Explanation (Code का विवरण)

  • Struct student: Contains roll number, name, and marks.
  • addStudent(): Adds a new student record to students.dat.
  • displayStudents(): Reads and displays all student records.
  • searchStudent(): Searches student by roll number.
  • deleteStudent(): Removes student record from file.
  • main(): Menu-driven system for student record operations.
  • Struct student: इसमें roll number, name और marks होते हैं।
  • addStudent(): नया student रिकॉर्ड students.dat में जोड़ता है।
  • displayStudents(): सभी students के records दिखाता है।
  • searchStudent(): दिए गए roll number से student को खोजता है।
  • deleteStudent(): student record को फाइल से हटाता है।
  • main(): मेन्यू आधारित सिस्टम जिससे आप operations कर सकते हैं।