Memory Lesson 16 of 16
C Final Project — Student Records with Structs and Files
Build a menu-driven student record program in C using structs, arrays, functions, pointers, input validation and file saving and loading.
This final project brings the whole course together. You'll build a student record system that adds students, lists them, finds the top student, and saves everything to a file so the data is still there next time.
Plan
- A
Studentstruct with a name and three grades. - An array of students and a counter.
- Functions for each menu option.
- Load records from
students.txtat startup and save them on exit. - A
do-whilemenu withswitch.
The code
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
#define FILE_NAME "students.txt"
typedef struct {
char name[40];
double math, science, english;
} Student;
Student list[MAX_STUDENTS];
int count = 0;
double average(const Student *s) {
return (s->math + s->science + s->english) / 3.0;
}
/* Reads a number from min to max, asking again on bad input. */
double read_number(const char *prompt, double min, double max) {
double value;
for (;;) {
printf("%s", prompt);
if (scanf("%lf", &value) == 1 && value >= min && value <= max) {
while (getchar() != '\n'); /* clear the rest of the line */
return value;
}
while (getchar() != '\n');
printf(" Please enter a number from %.0f to %.0f.\n", min, max);
}
}
void add_student(void) {
if (count == MAX_STUDENTS) {
printf("The list is full.\n");
return;
}
Student *s = &list[count];
printf("Name: ");
if (fgets(s->name, sizeof s->name, stdin) == NULL) return;
s->name[strcspn(s->name, "\n")] = '\0';
if (strlen(s->name) == 0) {
printf("Name can't be empty.\n");
return;
}
s->math = read_number("Math: ", 0, 100);
s->science = read_number("Science: ", 0, 100);
s->english = read_number("English: ", 0, 100);
count++;
printf("Added %s (average %.2f)\n", s->name, average(s));
}
void list_students(void) {
if (count == 0) {
printf("No students yet.\n");
return;
}
printf("%-20s %6s %6s %6s %8s\n", "NAME", "MATH", "SCI", "ENG", "AVERAGE");
for (int i = 0; i < count; i++) {
printf("%-20s %6.1f %6.1f %6.1f %8.2f\n", list[i].name,
list[i].math, list[i].science, list[i].english, average(&list[i]));
}
}
void show_top(void) {
if (count == 0) {
printf("No students yet.\n");
return;
}
int best = 0;
for (int i = 1; i < count; i++) {
if (average(&list[i]) > average(&list[best])) best = i;
}
printf("Top student: %s with %.2f\n", list[best].name, average(&list[best]));
}
void save_file(void) {
FILE *f = fopen(FILE_NAME, "w");
if (!f) {
printf("Could not save.\n");
return;
}
for (int i = 0; i < count; i++) {
fprintf(f, "%s|%.1f|%.1f|%.1f\n", list[i].name,
list[i].math, list[i].science, list[i].english);
}
fclose(f);
printf("Saved %d record(s) to %s\n", count, FILE_NAME);
}
void load_file(void) {
FILE *f = fopen(FILE_NAME, "r");
if (!f) return; /* first run: no file yet */
while (count < MAX_STUDENTS) {
Student *s = &list[count];
if (fscanf(f, " %39[^|]|%lf|%lf|%lf", s->name, &s->math, &s->science, &s->english) != 4)
break;
count++;
}
fclose(f);
printf("Loaded %d record(s).\n", count);
}
int main(void) {
load_file();
int choice;
do {
printf("\n=== STUDENT RECORDS ===\n");
printf("1. Add student\n2. List students\n3. Top student\n4. Save and exit\n");
choice = (int) read_number("Choose: ", 1, 4);
switch (choice) {
case 1: add_student(); break;
case 2: list_students(); break;
case 3: show_top(); break;
case 4: save_file(); break;
}
} while (choice != 4);
return 0;
}
Sample run
Output
=== STUDENT RECORDS === 1. Add student 2. List students 3. Top student 4. Save and exit Choose: 1 Name: Maria Clara Math: 94 Science: 92 English: 97 Added Maria Clara (average 94.33) === STUDENT RECORDS === ... Choose: 1 Name: Jose Rizal Math: 98 Science: 95 English: 99 Added Jose Rizal (average 97.33) Choose: 2 NAME MATH SCI ENG AVERAGE Maria Clara 94.0 92.0 97.0 94.33 Jose Rizal 98.0 95.0 99.0 97.33 Choose: 3 Top student: Jose Rizal with 97.33 Choose: 4 Saved 2 record(s) to students.txt
Run it again and the first line says "Loaded 2 record(s)."
How it works
| Part | Concepts |
|---|---|
Student + list[] | typedef struct, array of structs, #define |
average(const Student *s) | Pointer parameter, ->, const |
read_number | Loop + scanf return value + clearing input |
add_student | fgets, strcspn, pointer to an array element |
save_file / load_file | fopen modes, fprintf, fscanf with a scanset |
main | do-while menu with switch |
The file uses | as a separator because names contain spaces. %39[^|] is a scanset: "read up to 39 characters that are not |".
Challenges
- Add "Delete student" by number, shifting the remaining records down.
- Sort the list by average (bubble sort on structs, or
qsort). - Search by name using
strstr, ignoring capital letters. - Replace the fixed array with
malloc/reallocso there's no 100-student limit. - Export the list to
grades.csvfor Excel.
What's next?
Congratulations on finishing the C course! 🎉 You now understand memory, pointers and how programs really work. Next:
- The C++ course adds classes, strings and vectors on top of what you know.
- Try Arduino: microcontroller projects use C/C++ to control LEDs, sensors and motors.
- Data structures (linked lists, stacks, queues) are the natural next topic in C.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04