C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

File Handling in C

Employee Payroll System with Payslip File Export

Employee Payroll System so that every time you generate a payslip, it is also saved automatically as a .txt file (for example: Payslip_102.txt).

This allows employees (or users on your ITDeveloper.in site) to download or print their salary slips easily.

C Program: Employee Payroll System with Payslip File Export

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

struct Employee {

    int id;

    char name[50];

    float basicSalary;

    float hra, da, gross, tax, net;

};

 

// Function to calculate salary components

void calculateSalary(struct Employee *e) {

    e->hra = e->basicSalary * 0.20;   // 20% HRA

    e->da = e->basicSalary * 0.10;    // 10% DA

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

 

    // Simple tax logic

    if (e->gross > 50000)

        e->tax = e->gross * 0.10; // 10% tax

    else if (e->gross > 30000)

        e->tax = e->gross * 0.05; // 5% tax

    else

        e->tax = 0;

 

    e->net = e->gross - e->tax;

}

 

// Add employee record

void addEmployee() {

    FILE *fp;

    struct Employee e;

 

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

    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);

 

    calculateSalary(&e);

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

    fclose(fp);

 

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

}

 

// Display all employees

void displayEmployees() {

    FILE *fp;

    struct Employee e;

 

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

    if (!fp) {

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

        return;

    }

 

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

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

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

 

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

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

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

    }

 

    fclose(fp);

}

 

// Generate payslip and save to text file

void generatePayslip() {

    FILE *fp, *slipFile;

    struct Employee e;

    int id, found = 0;

    char filename[100];

 

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

    if (!fp) {

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

        return;

    }

 

    printf("\nEnter Employee ID to generate payslip: ");

    scanf("%d", &id);

 

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

        if (e.id == id) {

            found = 1;

 

            // Generate file name

            sprintf(filename, "Payslip_%d.txt", e.id);

            slipFile = fopen(filename, "w");

 

            if (!slipFile) {

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

                fclose(fp);

                return;

            }

 

            // Write formatted payslip to file

            fprintf(slipFile, "==============================================\n");

            fprintf(slipFile, "              EMPLOYEE PAYSLIP\n");

            fprintf(slipFile, "==============================================\n");

            fprintf(slipFile, "Employee ID     : %d\n", e.id);

            fprintf(slipFile, "Employee Name   : %s\n", e.name);

            fprintf(slipFile, "----------------------------------------------\n");

            fprintf(slipFile, "Basic Salary    : %.2f\n", e.basicSalary);

            fprintf(slipFile, "HRA (20%%)       : %.2f\n", e.hra);

            fprintf(slipFile, "DA (10%%)        : %.2f\n", e.da);

            fprintf(slipFile, "Gross Salary    : %.2f\n", e.gross);

            fprintf(slipFile, "Tax Deduction   : %.2f\n", e.tax);

            fprintf(slipFile, "----------------------------------------------\n");

            fprintf(slipFile, "Net Salary      : %.2f\n", e.net);

            fprintf(slipFile, "==============================================\n");

            fclose(slipFile);

 

            // Display message

            printf("\nPayslip generated successfully!\n");

            printf("Saved as: %s\n\n", filename);

 

            // Also print on screen

            printf("==============================================\n");

            printf("              EMPLOYEE PAYSLIP\n");

            printf("==============================================\n");

            printf("Employee ID     : %d\n", e.id);

            printf("Employee Name   : %s\n", e.name);

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

            printf("Basic Salary    : %.2f\n", e.basicSalary);

            printf("HRA (20%%)       : %.2f\n", e.hra);

            printf("DA (10%%)        : %.2f\n", e.da);

            printf("Gross Salary    : %.2f\n", e.gross);

            printf("Tax Deduction   : %.2f\n", e.tax);

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

            printf("Net Salary      : %.2f\n", e.net);

            printf("==============================================\n");

            break;

        }

    }

 

    if (!found)

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

 

    fclose(fp);

}

 

// 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. Generate Payslip (Save as File)\n");

        printf("4. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1: addEmployee(); break;

            case 2: displayEmployees(); break;

            case 3: generatePayslip(); break;

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

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

        }

    } while (choice != 4);

 

    return 0;

}

Output

 
OUTPUT :
Generated File Content (Payslip_102.txt)

==============================================
              EMPLOYEE PAYSLIP
==============================================
Employee ID     : 102
Employee Name   : Alice
----------------------------------------------
Basic Salary    : 45000.00
HRA (20%)       : 9000.00
DA (10%)        : 4500.00
Gross Salary    : 58500.00
Tax Deduction   : 5850.00
----------------------------------------------
Net Salary      : 52650.00
==============================================

How It Works

  1. When you choose Option 3: Generate Payslip,
    • The program looks up the employee by ID.
    • Creates a text file like Payslip_102.txt.
    • Writes the formatted salary details into it.
    • Displays the same payslip on screen.
  2. You can open this file in any text editor (Notepad, etc.) or even provide a download button if used in a web-based system.