🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101

Student database in C (file handling)

Add, search, update and delete student records saved in a binary file, the classic C mini project done properly.

Beginner⏱ 6–8 hoursđŸĒœ 7 stepsCStructsBinary files

What you'll learn

  • ◆structs and arrays of structs
  • ◆fread/fwrite and fseek for random access
  • ◆Safe input with fgets

What you'll build

  • ✓Add, list, search by roll number
  • ✓Update and delete records
  • ✓Sort by marks
  • ✓Data survives restarts

Step-by-step guide

  1. 1

    Define the record

    typedef struct { int roll; char name[50]; float marks; } Student; with a fixed size so records can be found by position.

  2. 2

    Add a record

    Open "students.dat" with "ab", fwrite(&s, sizeof(Student), 1, fp). Read the name with fgets and strip the newline (never use gets).

  3. 3

    List and search

    Open with "rb" and fread one record at a time until it returns 0; compare roll numbers.

  4. 4

    Update in place

    Open with "rb+", find the record, fseek(fp, -(long)sizeof(Student), SEEK_CUR) and fwrite the new data.

  5. 5

    Delete

    Copy every record except the deleted one into temp.dat, then remove() the old file and rename() temp.dat.

  6. 6

    Sort

    Load all records into a malloc'd array, qsort with a comparator on marks, print, then free().

  7. 7

    Menu and checks

    A do-while menu; check every fopen for NULL and every scanf return value.

🚀 Make it stand out

  • +Index file for faster search
  • +Password-protected admin menu
  • +Export to CSV