C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

File Handling in C

Student Record Management System using File Handling

C Program: Student Record Management System using File Handling

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

struct Student {

    int roll;

    char name[50];

    float marks;

};

 

// Function Prototypes

void addStudent();

void viewStudents();

void searchStudent();

void updateStudent();

void deleteStudent();

 

int main() {

    int choice;

 

    do {

        printf("\n===== Student Record Management System =====\n");

        printf("1. Add Student Record\n");

        printf("2. View All Students\n");

        printf("3. Search Student by Roll Number\n");

        printf("4. Update Student Record\n");

        printf("5. Delete Student Record\n");

        printf("6. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &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: printf("Exiting program...\n"); break;

            default: printf("Invalid choice! Try again.\n");

        }

    } while(choice != 6);

 

    return 0;

}

 

// Function to Add a Student

void addStudent() {

    FILE *file = fopen("students.txt", "a");

    struct Student s;

 

    if(file == NULL) {

        printf("Error opening file!\n");

        return;

    }

 

    printf("Enter Roll Number: ");

    scanf("%d", &s.roll);

    printf("Enter Name: ");

    scanf("%s", s.name);

    printf("Enter Marks: ");

    scanf("%f", &s.marks);

 

    fprintf(file, "%d %s %.2f\n", s.roll, s.name, s.marks);

    fclose(file);

 

    printf("Student record added successfully!\n");

}

 

// Function to View All Students

void viewStudents() {

    FILE *file = fopen("students.txt", "r");

    struct Student s;

 

    if(file == NULL) {

        printf("No records found!\n");

        return;

    }

 

    printf("\n--- Student Records ---\n");

    printf("Roll\tName\t\tMarks\n");

    printf("----------------------------------\n");

 

    while(fscanf(file, "%d %s %f", &s.roll, s.name, &s.marks) != EOF) {

        printf("%d\t%-10s\t%.2f\n", s.roll, s.name, s.marks);

    }

 

    fclose(file);

}

 

// Function to Search Student by Roll Number

void searchStudent() {

    FILE *file = fopen("students.txt", "r");

    struct Student s;

    int roll, found = 0;

 

    if(file == NULL) {

        printf("No records found!\n");

        return;

    }

 

    printf("Enter Roll Number to Search: ");

    scanf("%d", &roll);

 

    while(fscanf(file, "%d %s %f", &s.roll, s.name, &s.marks) != EOF) {

        if(s.roll == roll) {

            printf("Record Found:\n");

            printf("Roll: %d\nName: %s\nMarks: %.2f\n", s.roll, s.name, s.marks);

            found = 1;

            break;

        }

    }

 

    if(!found)

        printf("No record found for Roll No: %d\n", roll);

 

    fclose(file);

}

 

// Function to Update Student Record

void updateStudent() {

    FILE *file = fopen("students.txt", "r");

    FILE *temp = fopen("temp.txt", "w");

    struct Student s;

    int roll, found = 0;

 

    if(file == NULL) {

        printf("No records found!\n");

        return;

    }

 

    printf("Enter Roll Number to Update: ");

    scanf("%d", &roll);

 

    while(fscanf(file, "%d %s %f", &s.roll, s.name, &s.marks) != EOF) {

        if(s.roll == roll) {

            printf("Enter New Name: ");

            scanf("%s", s.name);

            printf("Enter New Marks: ");

            scanf("%f", &s.marks);

            found = 1;

        }

        fprintf(temp, "%d %s %.2f\n", s.roll, s.name, s.marks);

    }

 

    fclose(file);

    fclose(temp);

 

    remove("students.txt");

    rename("temp.txt", "students.txt");

 

    if(found)

        printf("Record updated successfully!\n");

    else

        printf("Record not found!\n");

}

 

// Function to Delete Student Record

void deleteStudent() {

    FILE *file = fopen("students.txt", "r");

    FILE *temp = fopen("temp.txt", "w");

    struct Student s;

    int roll, found = 0;

 

    if(file == NULL) {

        printf("No records found!\n");

        return;

    }

 

    printf("Enter Roll Number to Delete: ");

    scanf("%d", &roll);

 

    while(fscanf(file, "%d %s %f", &s.roll, s.name, &s.marks) != EOF) {

        if(s.roll != roll) {

            fprintf(temp, "%d %s %.2f\n", s.roll, s.name, s.marks);

        } else {

            found = 1;

        }

    }

 

    fclose(file);

    fclose(temp);

 

    remove("students.txt");

    rename("temp.txt", "students.txt");

 

    if(found)

        printf("Record deleted successfully!\n");

    else

        printf("Record not found!\n");

}

Output

 
OUTPUT :

===== Student Record Management System =====
1. Add Student Record
2. View All Students
3. Search Student by Roll Number
4. Update Student Record
5. Delete Student Record
6. Exit

Enter your choice: 1
Enter Roll Number: 101
Enter Name: Alice
Enter Marks: 89.5
Student record added successfully!

Enter your choice: 2
--- Student Records ---
Roll    Name            Marks
----------------------------------
101     Alice           89.50

Explanation

Step

Description

1

Opens students.txt in read mode using fopen("r").

2

Checks if the file exists — if not, prints an error.

3

Reads data from file using fscanf() and stores it in a Student structure.

4

Displays each student’s record in a tabular format.

5

Closes the file safely using fclose().