C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

File Handling in C

Store employee payroll system in file

A Payroll Management System using files is one of the most practical file-handling projects in C.

Below is a complete C program that allows you to store, display, search, update, and delete employee payroll records using file operations.

 

C Program: Employee Payroll Management Using File Handling

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

struct Employee {

    int id;

    char name[50];

    float basicSalary;

    float hra, da, gross;

};

 

// Function to calculate gross salary

float calculateGross(struct Employee *e) {

    e->hra = e->basicSalary * 0.20;

    e->da = e->basicSalary * 0.10;

    e->gross = e->basicSalary + e->hra + e->da;

    return e->gross;

}

 

// Function to add employee record

void addEmployee() {

    FILE *fp;

    struct Employee e;

    fp = fopen("payroll.dat", "ab"); // append binary mode

    if (!fp) {

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

        return;

    }

 

    printf("\nEnter Employee ID: ");

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

    printf("Enter Name: ");

    scanf("%s", e.name);

    printf("Enter Basic Salary: ");

    scanf("%f", &e.basicSalary);

 

    calculateGross(&e);

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

    fclose(fp);

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

}

 

// Function to display all employee records

void displayEmployees() {

    FILE *fp;

    struct Employee e;

    fp = fopen("payroll.dat", "rb");

    if (!fp) {

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

        return;

    }

 

    printf("\n--- Employee Payroll Records ---\n");

    printf("%-10s %-15s %-12s %-10s %-10s %-10s\n",

           "ID", "Name", "Basic", "HRA", "DA", "Gross");

 

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

        printf("%-10d %-15s %-12.2f %-10.2f %-10.2f %-10.2f\n",

               e.id, e.name, e.basicSalary, e.hra, e.da, e.gross);

    }

 

    fclose(fp);

}

 

// Function to search employee by ID

void searchEmployee() {

    FILE *fp;

    struct Employee e;

    int id, found = 0;

 

    fp = fopen("payroll.dat", "rb");

    if (!fp) {

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

        return;

    }

 

    printf("Enter Employee ID to search: ");

    scanf("%d", &id);

 

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

        if (e.id == id) {

            printf("\nEmployee Found!\n");

            printf("ID: %d\nName: %s\nBasic: %.2f\nHRA: %.2f\nDA: %.2f\nGross: %.2f\n",

                   e.id, e.name, e.basicSalary, e.hra, e.da, e.gross);

            found = 1;

            break;

        }

    }

 

    if (!found)

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

 

    fclose(fp);

}

 

// Function to update employee record

void updateEmployee() {

    FILE *fp;

    struct Employee e;

    int id, found = 0;

    long pos;

 

    fp = fopen("payroll.dat", "rb+");

    if (!fp) {

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

        return;

    }

 

    printf("Enter Employee ID to update: ");

    scanf("%d", &id);

 

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

        if (e.id == id) {

            found = 1;

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

 

            printf("Enter new Basic Salary: ");

            scanf("%f", &e.basicSalary);

            calculateGross(&e);

 

            fseek(fp, pos, SEEK_SET);

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

            printf("Employee record updated!\n");

            break;

        }

    }

 

    if (!found)

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

 

    fclose(fp);

}

 

// Function to delete an employee record

void deleteEmployee() {

    FILE *fp, *temp;

    struct Employee e;

    int id, found = 0;

 

    fp = fopen("payroll.dat", "rb");

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

 

    if (!fp || !temp) {

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

        return;

    }

 

    printf("Enter Employee ID to delete: ");

    scanf("%d", &id);

 

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

        if (e.id != id)

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

        else

            found = 1;

    }

 

    fclose(fp);

    fclose(temp);

 

    remove("payroll.dat");

    rename("temp.dat", "payroll.dat");

 

    if (found)

        printf("Employee record deleted successfully!\n");

    else

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

}

 

// Main Menu

int main() {

    int choice;

 

    do {

        printf("\n===== Employee Payroll System =====\n");

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

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

        printf("3. Search Employee by ID\n");

        printf("4. Update Employee Salary\n");

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

        printf("6. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1: addEmployee(); break;

            case 2: displayEmployees(); break;

            case 3: searchEmployee(); break;

            case 4: updateEmployee(); break;

            case 5: deleteEmployee(); break;

            case 6: printf("Exiting...\n"); break;

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

        }

    } while (choice != 6);

 

    return 0;

}

Output

 
OUTPUT :

===== Employee Payroll System =====
1. Add Employee
2. Display All Employees
3. Search Employee by ID
4. Update Employee Salary
5. Delete Employee
6. Exit
Enter your choice: 1

Enter Employee ID: 101
Enter Name: John
Enter Basic Salary: 50000

Employee added successfully!

Key Features

  • File-based Storage – Data is permanently stored in payroll.dat
  • CRUD Operations – Create, Read, Update, Delete employee records
  • Binary Mode – Uses fread() and fwrite() for efficient structure handling
  • Gross Salary Calculation – Automatically computed using HRA and DA