CASE STUDY 01 · C PROGRAMMING

Intermediate File-based CRUD

Student Record Management System

Design a menu-driven application that creates, reads, searches, updates and deletes validated student records using structures and binary files.

01 · PROBLEM DEFINITION

Why does this system exist?

A department coordinator repeatedly needs to register students, view the current list, locate one student, correct information and remove discontinued records. Paper registers and parallel arrays make these operations inconsistent. Our C application creates one authoritative record format and stores it beyond the current execution.

Engineering question: How can we preserve data safely while keeping every operation understandable to a beginner?

Actors and use cases

Coordinator

Adds, updates, searches and deletes records.

Student

Provides ID, name, branch and CGPA.

System

Validates, persists and reports results clearly.

Scope boundary

This educational version is a single-user command-line system. It does not include authentication, concurrent access or a network database. Those omissions are deliberate: the focus is structures, modular functions and reliable file replacement.

02 · REQUIREMENT ANALYSIS

Functional and quality requirements

FR-1 Add

Accept a unique positive ID, non-empty name and branch, and CGPA from 0.00 to 10.00.

FR-2 List

Read all stored records and print an aligned table plus total count.

FR-3 Search

Scan records by ID and show exactly one matching student.

FR-4 Update

Replace the non-key details of an existing student without changing the ID.

FR-5 Delete

Remove exactly one matching record and retain every other record.

FR-6 Exit

End cleanly without leaving files open or temporary files behind.

Non-functional requirements

  • Correctness: reject duplicates and invalid CGPA values.
  • Persistence: records remain available to later operations in the same environment.
  • Maintainability: one function owns one responsibility.
  • Recoverability: update/delete build a temporary replacement before touching the original.
  • Usability: prompts explain the expected range and errors explain the cause.
03 · DATA MODELLING

Represent one student as one structure

Student = { id: int, name: char[60], branch: char[20], cgpa: float }
FieldPurposeRuleWhy this type?
idStable lookup key> 0 and uniqueInteger comparison is simple and exact.
nameStudent identityNon-empty, up to 59 charsFixed array keeps the binary record size constant.
branchAcademic programmeNon-empty, up to 19 charsShort controlled text fits a fixed array.
cgpaAcademic performance0.0–10.0Decimal values require floating-point storage.
Key decision: ID is immutable during update. Allowing it to change could create duplicates and break its meaning as the lookup key.

Binary-file representation

fwrite(&s, sizeof s, 1, file) stores the complete structure as a fixed-size record. This makes sequential reading easy, but the file is tied to the structure layout and compiler representation. For portable data exchange, CSV or a database is better.

04 · MODULAR DESIGN

How data moves through the system

User selects operation
Input helper validates type
Business function checks rules
File operation executes
Clear result is displayed
ModuleResponsibilityMust not do
readInt / readFloat / readLineCollect clean inputMake record decisions
validStudentEnforce domain constraintsRead or write files
idExistsPrevent duplicate keysModify records
add/list/searchPerform direct CRUD operationsOwn the menu loop
update/deleteCreate safe replacement fileOverwrite source in place
main/menuCoordinate user choicesContain file-processing details
05 · ALGORITHM DESIGN

CRUD logic before code

Add algorithm

  1. Read the proposed ID and scan the file for a duplicate.
  2. Read name, branch and CGPA.
  3. Validate every field as one complete Student object.
  4. Open the binary file in append mode.
  5. Write exactly one structure and verify the write count.

Search algorithm

  1. Open the file in read-binary mode.
  2. Read one structure at a time using fread.
  3. Compare its ID with the target; return immediately on a match.
  4. If end-of-file is reached, report “not found”.

Safe update/delete pattern

Open original
Create temporary file
Copy or transform each record
Close both files
Replace original
The original is removed only after the full scan completes and both streams close. This avoids editing variable content directly inside the file.
06 · COMPLETE IMPLEMENTATION

Compiler-ready C11 program

Read the function boundaries first. Then copy the code, open the CodeBhavya compiler and test one operation at a time.

programs/student-record-system.c
Open Compiler
Loading source…

Reading guide

Start at main

Understand the endless menu loop and switch dispatch.

Follow one operation

Trace Add from input to validation to fwrite.

Compare mutation paths

Notice why update/delete need a temporary file but search does not.

07 · INTERACTIVE PROGRAM TRACING

Trace: adding ID 1001 and searching it

  1. User selects Add student.
  2. readInt stores ID 1001.
  3. Uniqueness check passes.
  4. Text fields are collected with fgets.
  5. CGPA is read and full object is validated.
  6. File opens in append-binary mode.
  7. Student 1001 is persisted.
  8. User chooses Search.
  9. Matching record is found.
  10. Search completes successfully.
Current state

Press Next to begin.

08 · TEST DESIGN

Test normal, boundary and failure paths

Test 1 — Valid add and list
Input sequence
1
1001
Bhavya Rao
CSE-AIML
8.70
2
6
Expected: “Student added successfully”, one aligned row, total records 1.
Test 2 — Duplicate ID
Add ID 1001 twice. The second attempt must stop before asking for the remaining fields and must not create a second record.
Test 3 — CGPA boundaries
Accept exactly 0 and 10. Reject -0.01 and 10.01 without writing a record.
Test 4 — Search missing ID
Search 9999 in a non-empty file. Expected: “Student not found” and no change to the file.
Test 5 — Update cancellation
Begin updating an existing ID and enter invalid CGPA. Expected: temporary file removed and original record unchanged.
Test 6 — Delete only record
Delete the sole record. The database remains valid but contains zero readable records.
09 · COMPLEXITY ANALYSIS

Cost as the record count grows

OperationTimeExtra spaceReason
Add with duplicate checkO(n)O(1)Existing IDs are scanned first.
ListO(n)O(1)Every record is read once.
SearchO(n)O(1)Sequential file has no index.
UpdateO(n)O(1)*All records are copied; disk temp is O(n).
DeleteO(n)O(1)*All retained records are copied.

*RAM usage is constant because one record is processed at a time; the temporary file requires O(n) disk space.

Scaling decision: At thousands of frequent searches, replace the sequential file with a sorted index, hash table or database.
10 · RELIABILITY & LIMITATIONS

What makes this safer—and what remains

Controls included

  • Input-type checks and buffer cleanup
  • Duplicate ID prevention
  • Full-object validation
  • Every opened file is closed
  • Update cancellation removes the temp file
  • Immediate return after a successful search

Production improvements

  • Use CSV/SQLite for portable records
  • Lock files during concurrent access
  • Write audit logs and backups
  • Use authenticated roles
  • Normalise branch values
  • Use atomic rename strategy supported by the OS
11 · PRACTICE & CHECKPOINTS

Prove that you understand the system

Which operations require a temporary replacement file?

Why is Student ID not edited?

Extension challenges

  1. Add semester, email and placement-status fields; update validation and table formatting.
  2. Create a report of students with CGPA greater than a user-supplied threshold.
  3. Sort records by CGPA without changing the stored order.
  4. Export all records to readable CSV.
  5. Add a confirmation step before deletion.
12 · INTERVIEW PREPARATION

Explain your design decisions

Why use a structure instead of parallel arrays?

A structure keeps all fields belonging to one student together. Passing, reading and writing one logical record becomes simpler, and indices cannot accidentally become misaligned across arrays.

Why use append mode for Add?

ab writes a new fixed-size record at the end without rewriting existing records. The duplicate scan happens before opening the append stream.

Why not update directly with fseek?

Fixed-size records can be updated in place, but a temporary-file approach unifies update and delete, is easier to reason about, and avoids leaving half-written record content after a failed operation.

What is the largest limitation of binary structure files?

The representation can change with compiler padding, field sizes and architecture. It is not a stable interchange format across unrelated systems.

How would you support fast search?

Maintain an in-memory hash index from ID to file offset, keep records sorted for binary search, or move persistence to an indexed database.

13 · KEY TAKEAWAY

One program connects the complete C foundation

This system combines input handling, functions, structures, validation, switch-based control flow, sequential searching and persistent files. More importantly, it demonstrates an engineering habit: validate before mutation and preserve the original until the replacement is ready.