C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

File Handling in C

Implement mini database using file handling

Complete mini database implementation in C using file handling.


It lets you add, display, search, update, and delete records (like a simple persistent database stored in a file).

We’ll build it as a menu-driven program that manages a list of records (like student, employee, or customer info).

C Program: Mini Database using File Handling in C

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

#define FILENAME "database.dat"

 

// Define record structure

struct Record {

    int id;

    char name[50];

    int age;

    float salary;

};

 

// Function prototypes

void addRecord();

void displayRecords();

void searchRecord();

void updateRecord();

void deleteRecord();

 

int main() {

    int choice;

 

    while (1) {

        printf("\n========= MINI DATABASE SYSTEM =========\n");

        printf("1. Add Record\n");

        printf("2. Display All Records\n");

        printf("3. Search Record\n");

        printf("4. Update Record\n");

        printf("5. Delete Record\n");

        printf("6. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1: addRecord(); break;

            case 2: displayRecords(); break;

            case 3: searchRecord(); break;

            case 4: updateRecord(); break;

            case 5: deleteRecord(); break;

            case 6: exit(0);

            default: printf("Invalid choice!\n");

        }

    }

    return 0;

}

 

// Function to add a new record

void addRecord() {

    FILE *fp;

    struct Record rec;

 

    fp = fopen(FILENAME, "ab"); // append binary mode

    if (fp == NULL) {

        printf("Error opening file!\n");

        return;

    }

 

    printf("Enter ID: ");

    scanf("%d", &rec.id);

    printf("Enter Name: ");

    getchar(); // clear newline

    fgets(rec.name, sizeof(rec.name), stdin);

    rec.name[strcspn(rec.name, "\n")] = '\0'; // remove newline

    printf("Enter Age: ");

    scanf("%d", &rec.age);

    printf("Enter Salary: ");

    scanf("%f", &rec.salary);

 

    fwrite(&rec, sizeof(rec), 1, fp);

    fclose(fp);

 

    printf("Record added successfully!\n");

}

 

// Function to display all records

void displayRecords() {

    FILE *fp;

    struct Record rec;

 

    fp = fopen(FILENAME, "rb");

    if (fp == NULL) {

        printf("No records found!\n");

        return;

    }

 

    printf("\n%-10s %-20s %-10s %-10s\n", "ID", "Name", "Age", "Salary");

    printf("---------------------------------------------------\n");

 

    while (fread(&rec, sizeof(rec), 1, fp)) {

        printf("%-10d %-20s %-10d %-10.2f\n", rec.id, rec.name, rec.age, rec.salary);

    }

 

    fclose(fp);

}

 

// Function to search for a record by ID

void searchRecord() {

    FILE *fp;

    struct Record rec;

    int id, found = 0;

 

    fp = fopen(FILENAME, "rb");

    if (fp == NULL) {

        printf("File not found!\n");

        return;

    }

 

    printf("Enter ID to search: ");

    scanf("%d", &id);

 

    while (fread(&rec, sizeof(rec), 1, fp)) {

        if (rec.id == id) {

            printf("\nRecord Found:\n");

            printf("ID: %d\nName: %s\nAge: %d\nSalary: %.2f\n",

                   rec.id, rec.name, rec.age, rec.salary);

            found = 1;

            break;

        }

    }

 

    if (!found)

        printf("Record not found!\n");

 

    fclose(fp);

}

 

// Function to update a record by ID

void updateRecord() {

    FILE *fp;

    struct Record rec;

    int id, found = 0;

    long pos;

 

    fp = fopen(FILENAME, "r+b"); // read/write binary mode

    if (fp == NULL) {

        printf("File not found!\n");

        return;

    }

 

    printf("Enter ID to update: ");

    scanf("%d", &id);

 

    while (fread(&rec, sizeof(rec), 1, fp)) {

        if (rec.id == id) {

            printf("Enter new Name: ");

            getchar(); // clear newline

            fgets(rec.name, sizeof(rec.name), stdin);

            rec.name[strcspn(rec.name, "\n")] = '\0';

            printf("Enter new Age: ");

            scanf("%d", &rec.age);

            printf("Enter new Salary: ");

            scanf("%f", &rec.salary);

 

            pos = ftell(fp) - sizeof(rec);

            fseek(fp, pos, SEEK_SET);

            fwrite(&rec, sizeof(rec), 1, fp);

 

            printf("Record updated successfully!\n");

            found = 1;

            break;

        }

    }

 

    if (!found)

        printf("Record not found!\n");

 

    fclose(fp);

}

 

// Function to delete a record by ID

void deleteRecord() {

    FILE *fp, *temp;

    struct Record rec;

    int id, found = 0;

 

    fp = fopen(FILENAME, "rb");

    if (fp == NULL) {

        printf("File not found!\n");

        return;

    }

 

    temp = fopen("temp.dat", "wb");

    if (temp == NULL) {

        printf("Error creating temp file!\n");

        fclose(fp);

        return;

    }

 

    printf("Enter ID to delete: ");

    scanf("%d", &id);

 

    while (fread(&rec, sizeof(rec), 1, fp)) {

        if (rec.id == id) {

            found = 1;

        } else {

            fwrite(&rec, sizeof(rec), 1, temp);

        }

    }

 

    fclose(fp);

    fclose(temp);

 

    remove(FILENAME);

    rename("temp.dat", FILENAME);

 

    if (found)

        printf("Record deleted successfully!\n");

    else

        printf("Record not found!\n");

}

Output

 
OUTPUT :

========= MINI DATABASE SYSTEM =========
1. Add Record
2. Display All Records
3. Search Record
4. Update Record
5. Delete Record
6. Exit
Enter your choice: 1

Enter ID: 101
Enter Name: Alice
Enter Age: 25
Enter Salary: 50000
Record added successfully!

Concepts Demonstrated

  • Binary file handling (fopen, fread, fwrite, fseek)
  • Persistent storage (records remain after program exits)
  • CRUD operations:
    • Create → addRecord()
    • Read → displayRecords() and searchRecord()
    • Update → updateRecord()
    • Delete → deleteRecord()
  • Structure-based data storage