Skip to content

Instantly share code, notes, and snippets.

@abhinayp3000-byte
Created December 6, 2025 06:19
Show Gist options
  • Select an option

  • Save abhinayp3000-byte/cf13ce4695cc020329a40f9ecad94668 to your computer and use it in GitHub Desktop.

Select an option

Save abhinayp3000-byte/cf13ce4695cc020329a40f9ecad94668 to your computer and use it in GitHub Desktop.
This project is a simple yet functional Student Record Management System developed in C language using file handling. It allows different user roles such as Admin, Staff, and Guest to manage and view student records with restricted access.
abhi 1234 Admin
staff1 1111 Staff
guest1 0000 Guest
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STUDENT_FILE "students.txt"
#define CREDENTIAL_FILE "credentials.txt"
#define MAX_NAME 50
#define MAX_USER 20
#define MAX_ROLE 10
#define MAX_PASS 20
struct Student {
int roll;
char name[MAX_NAME];
float marks;
};
char currentUser[MAX_USER];
char currentRole[MAX_ROLE];
int loginSystem();
void mainMenu();
void addStudent();
void displayStudents();
void searchStudent();
void updateStudent();
void deleteStudent();
void clearInputBuffer();
int main() {
printf("=========================================\n");
printf(" STUDENT RECORD MANAGEMENT SYSTEM\n");
printf("=========================================\n");
while (!loginSystem()) {
printf("\nLogin failed. Please try again.\n\n");
}
printf("\nLogin successful! Welcome, %s (%s).\n", currentUser, currentRole);
mainMenu();
printf("\nThank you for using SRMS. Goodbye!\n");
return 0;
}
int loginSystem() {
char username[MAX_USER];
char password[MAX_PASS];
char fileUser[MAX_USER];
char filePass[MAX_PASS];
char fileRole[MAX_ROLE];
int found = 0;
printf("\n----------- LOGIN -----------\n");
printf("Username: ");
scanf("%s", username);
printf("Password: ");
scanf("%s", password);
FILE *fp = fopen(CREDENTIAL_FILE, "r");
if (fp == NULL) {
printf("Error: Could not open %s\n", CREDENTIAL_FILE);
return 0;
}
while (fscanf(fp, "%s %s %s", fileUser, filePass, fileRole) == 3) {
if (strcmp(username, fileUser) == 0 && strcmp(password, filePass) == 0) {
strcpy(currentUser, fileUser);
strcpy(currentRole, fileRole);
found = 1;
break;
}
}
fclose(fp);
if (!found) {
printf("Invalid username or password.\n");
return 0;
}
return 1;
}
void mainMenu() {
int choice;
if (strcmp(currentRole, "Admin") == 0) {
do {
printf("\n========== ADMIN MENU ==========\n");
printf("1. Add Student\n");
printf("2. Display All Students\n");
printf("3. Search Student\n");
printf("4. Update Student\n");
printf("5. Delete Student\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: addStudent(); break;
case 2: displayStudents(); break;
case 3: searchStudent(); break;
case 4: updateStudent(); break;
case 5: deleteStudent(); break;
case 0: printf("Exiting...\n"); break;
default: printf("Invalid choice. Please try again.\n");
}
} while (choice != 0);
} else if (strcmp(currentRole, "Staff") == 0) {
do {
printf("\n========== STAFF MENU ==========\n");
printf("1. Display All Students\n");
printf("2. Search Student\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: displayStudents(); break;
case 2: searchStudent(); break;
case 0: printf("Exiting...\n"); break;
default: printf("Invalid choice. Please try again.\n");
}
} while (choice != 0);
} else if (strcmp(currentRole, "Guest") == 0) {
do {
printf("\n========== GUEST MENU ==========\n");
printf("1. Display All Students\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: displayStudents(); break;
case 0: printf("Exiting...\n"); break;
default: printf("Invalid choice. Please try again.\n");
}
} while (choice != 0);
} else {
printf("Error: Unknown role '%s'. Exiting.\n", currentRole);
}
}
void addStudent() {
if (strcmp(currentRole, "Admin") != 0) {
printf("Access denied.\n");
return;
}
struct Student s;
FILE *fp = fopen(STUDENT_FILE, "a");
if (fp == NULL) {
printf("Error: Could not open %s\n", STUDENT_FILE);
return;
}
printf("\n------ ADD STUDENT ------\n");
printf("Enter Roll Number: ");
scanf("%d", &s.roll);
printf("Enter Name (no spaces): ");
scanf("%s", s.name);
printf("Enter Marks: ");
scanf("%f", &s.marks);
fprintf(fp, "%d %s %.2f\n", s.roll, s.name, s.marks);
fclose(fp);
printf("Student record added successfully.\n");
}
void displayStudents() {
struct Student s;
FILE *fp = fopen(STUDENT_FILE, "r");
if (fp == NULL) {
printf("No student records found (%s not found).\n", STUDENT_FILE);
return;
}
printf("\n------ STUDENT LIST ------\n");
printf("%-10s %-20s %-10s\n", "Roll", "Name", "Marks");
printf("------------------------------------------\n");
int count = 0;
while (fscanf(fp, "%d %s %f", &s.roll, s.name, &s.marks) == 3) {
printf("%-10d %-20s %-10.2f\n", s.roll, s.name, s.marks);
count++;
}
if (count == 0) {
printf("No records to display.\n");
}
fclose(fp);
}
void searchStudent() {
int rollToSearch;
struct Student s;
int found = 0;
FILE *fp = fopen(STUDENT_FILE, "r");
if (fp == NULL) {
printf("No student records found.\n");
return;
}
printf("\nEnter Roll Number to search: ");
scanf("%d", &rollToSearch);
while (fscanf(fp, "%d %s %f", &s.roll, s.name, &s.marks) == 3) {
if (s.roll == rollToSearch) {
printf("Record found:\n");
printf("Roll : %d\n", s.roll);
printf("Name : %s\n", s.name);
printf("Marks : %.2f\n", s.marks);
found = 1;
break;
}
}
if (!found) {
printf("No record found with Roll Number %d\n", rollToSearch);
}
fclose(fp);
}
void updateStudent() {
if (strcmp(currentRole, "Admin") != 0) {
printf("Access denied.\n");
return;
}
int rollToUpdate;
struct Student s;
int found = 0;
FILE *fp = fopen(STUDENT_FILE, "r");
FILE *temp = fopen("temp.txt", "w");
if (fp == NULL || temp == NULL) {
printf("Error opening file.\n");
return;
}
printf("\nEnter Roll Number to update: ");
scanf("%d", &rollToUpdate);
while (fscanf(fp, "%d %s %f", &s.roll, s.name, &s.marks) == 3) {
if (s.roll == rollToUpdate) {
printf("Existing Record: Roll=%d, Name=%s, Marks=%.2f\n", s.roll, s.name, s.marks);
printf("Enter new Name (no spaces): ");
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(fp);
fclose(temp);
remove(STUDENT_FILE);
rename("temp.txt", STUDENT_FILE);
if (found) {
printf("Student record updated successfully.\n");
} else {
printf("No record found with Roll Number %d\n", rollToUpdate);
}
}
void deleteStudent() {
if (strcmp(currentRole, "Admin") != 0) {
printf("Access denied.\n");
return;
}
int rollToDelete;
struct Student s;
int found = 0;
FILE *fp = fopen(STUDENT_FILE, "r");
FILE *temp = fopen("temp.txt", "w");
if (fp == NULL || temp == NULL) {
printf("Error opening file.\n");
return;
}
printf("\nEnter Roll Number to delete: ");
scanf("%d", &rollToDelete);
while (fscanf(fp, "%d %s %f", &s.roll, s.name, &s.marks) == 3) {
if (s.roll != rollToDelete) {
fprintf(temp, "%d %s %.2f\n", s.roll, s.name, s.marks);
} else {
found = 1;
}
}
fclose(fp);
fclose(temp);
remove(STUDENT_FILE);
rename("temp.txt", STUDENT_FILE);
if (found) {
printf("Student record deleted successfully.\n");
} else {
printf("No record found with Roll Number %d\n", rollToDelete);
}
}
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF) {}
}
2401 abhi 95.00
2402 dhanush 92.00
2403 faiz 90.00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment