Student Record System in C
Almost every real application stores records: students in a school, books in a library, customers in a bank. The core operations are always the same: Create, Read, Update, Delete (CRUD). In this project you build a student record management system that keeps records in a binary file, so they are still there after the program closes.
You will add students, list them, search by roll number, update marks and delete a record, using a struct, fwrite, fread, fseek and a temporary file. A second program reads the file and prints a class report with the topper and average.
What You Will Learn
- How to design a record with a structure.
- How to append records to a binary file with
fwrite. - How to read all records with
freadin a loop. - How to update a record in place using
fseek. - How to delete a record using a temporary file.
- How to generate a report from stored data.
Student Record System in C Explained
Exam definition: A record management system stores data as fixed-size records (structures) in a file and supports the four CRUD operations: Create (append a record with fwrite), Read (read records sequentially with fread), Update (find the record, move back one record with fseek and overwrite it) and Delete (copy every other record to a temporary file and replace the original).
Memory trick: The binary file is a register book in the school office, where every student gets one line of the same length. Add = write on the next empty line. Search = read line by line. Update = find the line and write over it. Delete = copy the whole register into a new book, skipping that student's line.
Syntax: CRUD with a Binary File
struct student { int roll; char name[30]; float marks; };
struct student s;
/* Create: append */
fp = fopen("students.dat", "ab"); fwrite(&s, sizeof s, 1, fp);
/* Read: all records */
fp = fopen("students.dat", "rb"); while (fread(&s, sizeof s, 1, fp) == 1) { ... }
/* Update: overwrite the record just read */
fp = fopen("students.dat", "rb+");
while (fread(&s, sizeof s, 1, fp) == 1)
if (s.roll == target) { s.marks = new_marks;
fseek(fp, -(long)sizeof s, SEEK_CUR);
fwrite(&s, sizeof s, 1, fp); break; }
/* Delete: copy all except one to temp.dat, then rename */
remove("students.dat"); rename("temp.dat", "students.dat");
Example 1: Student Record Management System
#include <stdio.h>
#define FILE_NAME "students.dat"
struct student
{
int roll;
char name[30];
float marks;
};
void add_student(void)
{
struct student s;
FILE *fp;
printf("Enter roll, name and marks: ");
if (scanf("%d %29s %f", &s.roll, s.name, &s.marks) != 3)
return;
fp = fopen(FILE_NAME, "ab");
if (fp == NULL) { printf("Cannot open file\n"); return; }
fwrite(&s, sizeof s, 1, fp);
fclose(fp);
printf("Record added.\n");
}
void display_all(void)
{
struct student s;
FILE *fp = fopen(FILE_NAME, "rb");
int count = 0;
if (fp == NULL) { printf("No records yet.\n"); return; }
printf("%-6s %-10s %s\n", "Roll", "Name", "Marks");
while (fread(&s, sizeof s, 1, fp) == 1)
{
printf("%-6d %-10s %.1f\n", s.roll, s.name, s.marks);
count++;
}
printf("%d record(s) in file\n", count);
fclose(fp);
}
void search_student(void)
{
struct student s;
FILE *fp = fopen(FILE_NAME, "rb");
int roll, found = 0;
printf("Enter roll to search: ");
if (scanf("%d", &roll) != 1 || fp == NULL) { if (fp) fclose(fp); return; }
while (fread(&s, sizeof s, 1, fp) == 1)
if (s.roll == roll)
{
printf("Found: %d %s %.1f\n", s.roll, s.name, s.marks);
found = 1;
break;
}
if (!found)
printf("Roll %d not found.\n", roll);
fclose(fp);
}
void update_marks(void)
{
struct student s;
FILE *fp = fopen(FILE_NAME, "rb+");
int roll;
float marks;
printf("Enter roll to update: ");
if (scanf("%d", &roll) != 1 || fp == NULL) { if (fp) fclose(fp); return; }
while (fread(&s, sizeof s, 1, fp) == 1)
if (s.roll == roll)
{
printf("New marks for %s: ", s.name);
if (scanf("%f", &marks) != 1) break;
s.marks = marks;
fseek(fp, -(long) sizeof s, SEEK_CUR); /* back to the start of this record */
fwrite(&s, sizeof s, 1, fp);
printf("Record updated.\n");
fclose(fp);
return;
}
printf("Roll %d not found.\n", roll);
fclose(fp);
}
void delete_student(void)
{
struct student s;
FILE *fp = fopen(FILE_NAME, "rb"), *tmp;
int roll, found = 0;
printf("Enter roll to delete: ");
if (scanf("%d", &roll) != 1 || fp == NULL) { if (fp) fclose(fp); return; }
tmp = fopen("temp.dat", "wb");
if (tmp == NULL) { fclose(fp); return; }
while (fread(&s, sizeof s, 1, fp) == 1)
{
if (s.roll == roll)
found = 1; /* skip it */
else
fwrite(&s, sizeof s, 1, tmp);
}
fclose(fp);
fclose(tmp);
remove(FILE_NAME);
rename("temp.dat", FILE_NAME);
printf(found ? "Record deleted.\n" : "Roll not found.\n");
}
int main(void)
{
int choice;
while (1)
{
printf("\n1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit\nChoice: ");
if (scanf("%d", &choice) != 1 || choice == 0)
break;
switch (choice)
{
case 1: add_student(); break;
case 2: display_all(); break;
case 3: search_student(); break;
case 4: update_marks(); break;
case 5: delete_student(); break;
default: printf("Invalid choice\n");
}
}
printf("Goodbye!\n");
return 0;
}
Output
1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 1 Enter roll, name and marks: 101 Aarav 82.5 Record added. 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 1 Enter roll, name and marks: 102 Diya 91 Record added. 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 1 Enter roll, name and marks: 103 Kabir 67 Record added. 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 2 Roll Name Marks 101 Aarav 82.5 102 Diya 91.0 103 Kabir 67.0 3 record(s) in file 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 3 Enter roll to search: 102 Found: 102 Diya 91.0 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 4 Enter roll to update: 103 New marks for Kabir: 72.5 Record updated. 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 5 Enter roll to delete: 101 Record deleted. 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 2 Roll Name Marks 102 Diya 91.0 103 Kabir 72.5 2 record(s) in file 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 3 Enter roll to search: 101 Roll 101 not found. 1.Add 2.List 3.Search 4.Update 5.Delete 0.Exit Choice: 0 Goodbye!
Code Explanation
struct studentis a fixed-size record, so every record takes exactlysizeof(struct student)bytes in the file.- Add opens the file in
"ab"(append binary) mode, so new records go at the end and old ones are kept. The file is created if it doesn't exist. - List and Search read one record at a time with
freaduntil it returns less than 1 (end of file). - Update opens in
"rb+"(read and write). After reading the matching record, the file position is just past it, sofseek(fp, -(long)sizeof s, SEEK_CUR)moves back one record beforefwriteoverwrites it. - Delete copies every record except the matching one into
temp.dat, then deletes the old file and renames the temporary one. A file can't simply have a hole cut out of its middle. - Each function opens and closes the file itself, so the data on disk is always up to date, even if the program crashes later.
How the Records Sit in the File
students.dat (each record = sizeof(struct student) bytes, e.g. 40)
offset 0 offset 40 offset 80
┌──────────┬────────────┬────────────┐
│ 101 Aarav│ 102 Diya │ 103 Kabir │
└──────────┴────────────┴────────────┘
Update roll 103: read records until 103 is found (position now 120),
fseek back 40 bytes to 80, write the changed record over the old one.
Because every record has the same size, record number k starts at byte k * sizeof(struct student). You could even jump straight to it with fseek(fp, k * sizeof s, SEEK_SET).
Example 2: Class Report from the File
#include <stdio.h>
struct student
{
int roll;
char name[30];
float marks;
};
int main(void)
{
struct student list[] = {
{101, "Aarav", 82.5f}, {102, "Diya", 91.0f}, {103, "Kabir", 67.0f},
{104, "Meera", 38.5f}, {105, "Rohan", 74.0f}
};
struct student s, top;
FILE *fp;
int count = 0, passed = 0;
float total = 0;
fp = fopen("students.dat", "wb"); /* sample data */
if (fp == NULL)
return 1;
fwrite(list, sizeof list[0], 5, fp); /* write 5 records at once */
fclose(fp);
fp = fopen("students.dat", "rb");
if (fp == NULL)
return 1;
while (fread(&s, sizeof s, 1, fp) == 1)
{
if (count == 0 || s.marks > top.marks)
top = s;
total += s.marks;
if (s.marks >= 40)
passed++;
count++;
}
fclose(fp);
printf("===== CLASS REPORT =====\n");
printf("Students : %d\n", count);
printf("Class average: %.2f\n", total / count);
printf("Passed : %d, Failed: %d\n", passed, count - passed);
printf("Topper : %s (roll %d) with %.1f\n", top.name, top.roll, top.marks);
remove("students.dat");
return 0;
}
Output
===== CLASS REPORT ===== Students : 5 Class average: 70.60 Passed : 4, Failed: 1 Topper : Diya (roll 102) with 91.0
fwrite(list, sizeof list[0], 5, fp)writes a whole array of records in one call.- The report reads every record once and keeps running totals: count, sum, pass count and the best student so far.
- Average = (82.5 + 91 + 67 + 38.5 + 74) / 5 = 353 / 5 = 70.60.
Important Points to Remember
- Use a structure for each record; every record has the same size in a binary file.
- "ab" to add, "rb" to read, "rb+" to update, "wb" to create a fresh file.
- Loop with while (fread(...) == 1), not
while (!feof(fp)). - Update in place: fseek back one record with
SEEK_CUR, then fwrite. - Delete: copy all other records to a temporary file, then remove and rename.
- Always check fopen and close every file you open.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Opening with "wb" to add a record | All old records are erased | Use "ab" to append |
| Updating without fseek | The next record is overwritten instead | fseek(fp, -(long)sizeof s, SEEK_CUR) |
| Reading binary data with fscanf | Garbage values | Use fread for binary files |
| Renaming before closing the files | Rename/remove fails on some systems | fclose both files first |
scanf("%s") for names with spaces | "Aarav Shah" is split into two inputs | Use one-word names, or fgets for full names |
Student Record System Practice Questions
- What does CRUD stand for?
- Which file mode is used to add a record without losing old ones?
- Explain how a record is updated in place.
- Why is a temporary file used to delete a record?
- Why is
while (fread(&s, sizeof s, 1, fp) == 1)the correct loop?
Show answers
- Create, Read, Update, Delete.
- "ab" (append binary).
- Open with "rb+", read records until the match, fseek back by sizeof(record) from the current position, then fwrite the modified record.
- Bytes can't be removed from the middle of a file, so all other records are copied to a new file which then replaces the original.
- fread returns the number of records read; it stops exactly at the end of the file or on an error, unlike checking feof first.
Coding Exercise
Write a function that prints only the students who scored above the class average. Use the five students from Example 2 (write them to the file first, then read the file twice: once for the average, once to print).
Show solution
#include <stdio.h>
struct student
{
int roll;
char name[30];
float marks;
};
int main(void)
{
struct student list[] = {
{101, "Aarav", 82.5f}, {102, "Diya", 91.0f}, {103, "Kabir", 67.0f},
{104, "Meera", 38.5f}, {105, "Rohan", 74.0f}
};
struct student s;
FILE *fp = fopen("students.dat", "wb");
float total = 0, avg;
int count = 0;
if (fp == NULL) return 1;
fwrite(list, sizeof list[0], 5, fp);
fclose(fp);
fp = fopen("students.dat", "rb");
if (fp == NULL) return 1;
while (fread(&s, sizeof s, 1, fp) == 1) /* pass 1: average */
{
total += s.marks;
count++;
}
avg = total / count;
printf("Class average: %.2f\nAbove average:\n", avg);
rewind(fp); /* pass 2: print */
while (fread(&s, sizeof s, 1, fp) == 1)
if (s.marks > avg)
printf(" %d %s %.1f\n", s.roll, s.name, s.marks);
fclose(fp);
remove("students.dat");
return 0;
}
Class average: 70.60 Above average: 101 Aarav 82.5 102 Diya 91.0 105 Rohan 74.0
Quick Quiz: Student Record System (MCQ)
-
Which mode appends records to a binary file?
A) "rb" B) "wb" C) "ab" D) "r+"
-
fread(&s, sizeof s, 1, fp)returns 1 when:A) A record was read B) The file is empty C) An error occurred D) Always
-
To move back one record before overwriting, use:
A) rewind(fp) B) fseek(fp, -(long)sizeof s, SEEK_CUR) C) ftell(fp) D) fclose(fp)
-
Which mode allows reading and updating an existing file?
A) "ab" B) "wb" C) "rb+" D) "w"
-
After copying records to temp.dat, which call replaces the old file?
A) rename("temp.dat", "students.dat") (after removing the old file) B) fcopy C) fmove D) strcpy
Show answers
- C: "ab".
- A: a record was read.
- B: fseek back by one record.
- C: "rb+".
- A: rename, after removing the old file.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| CRUD | Create, Read, Update, Delete |
|---|---|
| Record | A struct, fixed size |
| Add | "ab" + fwrite |
| Read all | while (fread(...) == 1) |
| Update | "rb+", fseek back, fwrite |
| Delete | Temp file, remove, rename |
| Record k | Starts at byte k × sizeof(record) |
Frequently asked questions
- How do you store student records permanently in C?
- Write each struct to a binary file with fwrite and read it back with fread.
- How do you delete a record from a file in C?
- Copy every other record to a temporary file, delete the original and rename the temporary file.
- How do you modify a record in a binary file?
- Open with "rb+", find the record, fseek back one record size and fwrite the new data.
Remember
- Binary file = register book with equal-length lines.
- Delete = copy without the line.
Next Topic
Next lesson: Library Management System in C. You will apply the same ideas to books: issue, return and track availability in a library system.