Skip to main content

Employee text file; sort into binary file

Short answer

Write employee records to text file, read into array, sort by ascending salary, then fwrite sorted array to binary file sorted.dat. Search binary file by name; output name, ID, salary, and rank (position in sorted order, starting from lowest salary).

Write a C program to store employee details in a text file. Read data from the text file, sort them in ascending order of salary and store the sorted record to a binary file. Display the details and rank of employee given by the user.

Steps.

  1. Read n employees (name, id, salary) and store them in a text file employee.txt with fprintf().
  2. Read the text file back into an array of structures with fscanf().
  3. Sort the array in ascending order of salary (bubble sort, swapping whole structures).
  4. Write the sorted array to a binary file sorted.dat using fwrite().
  5. Read the binary file with fread(). The user gives an employee name; the program displays that employee's details and rank. Assumption: rank is the position in the sorted file, so rank 1 is the lowest salary (the question does not say whether the user gives a name or a rank; searching by name is used here).
#include <stdio.h>
#include <string.h>
#define MAX 100

struct employee {
    char name[30];
    int id;
    float salary;
};

int main(void)
{
    struct employee e[MAX], t;
    FILE *fp;
    int n, i, j, found = 0;
    char key[30];

    /* 1. store details in a text file */
    fp = fopen("employee.txt", "w");
    if (fp == NULL) { printf("Cannot open employee.txt\n"); return 1; }
    printf("How many employees? ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("Employee %d (name id salary): ", i + 1);
        scanf("%29s %d %f", t.name, &t.id, &t.salary);
        fprintf(fp, "%s %d %f\n", t.name, t.id, t.salary);
    }
    fclose(fp);

    /* 2. read them back */
    fp = fopen("employee.txt", "r");
    if (fp == NULL) { printf("Cannot open employee.txt\n"); return 1; }
    for (i = 0; i < n; i++)
        fscanf(fp, "%29s %d %f", e[i].name, &e[i].id, &e[i].salary);
    fclose(fp);

    /* 3. sort by salary (ascending) */
    for (i = 0; i < n - 1; i++)
        for (j = 0; j < n - 1 - i; j++)
            if (e[j].salary > e[j + 1].salary) {
                t = e[j]; e[j] = e[j + 1]; e[j + 1] = t;
            }

    /* 4. store sorted records in a binary file */
    fp = fopen("sorted.dat", "wb");
    if (fp == NULL) { printf("Cannot open sorted.dat\n"); return 1; }
    fwrite(e, sizeof(struct employee), n, fp);
    fclose(fp);

    /* 5. read the binary file and show details and rank of a given employee */
    fp = fopen("sorted.dat", "rb");
    if (fp == NULL) { printf("Cannot open sorted.dat\n"); return 1; }
    printf("Enter name of employee: ");
    scanf("%29s", key);
    for (i = 1; fread(&t, sizeof t, 1, fp) == 1; i++)
        if (strcmp(t.name, key) == 0) {
            printf("Name: %s  ID: %d  Salary: %.2f  Rank: %d of %d\n",
                   t.name, t.id, t.salary, i, n);
            found = 1;
        }
    if (!found) printf("Employee not found\n");
    fclose(fp);
    return 0;
}

Sample run (Hari earns the highest salary of the four)

Input: 4 Ram 101 45000 Sita 102 32000 Hari 103 58000 Gita 104 39000 Hari

How many employees? Employee 1 (name id salary): Employee 2 (name id salary): Employee 3 (name id salary): Employee 4 (name id salary): Enter name of employee: Name: Hari  ID: 103  Salary: 58000.00  Rank: 4 of 4

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 10 questions →