---
title: "Employee text file; sort into binary file"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-employee-sorted-binary"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-employee-sorted-binary
site: BCT Engineering
---

# Employee text file; sort into binary file

Computer Programming · Chapter 10: Data Files

- Asked in: 2071 Shrawan, 8 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-employee-sorted-binary
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

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.

## 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).

## Answer

**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).

```c
#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`

```text
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
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. While sorting employees by salary, why does the program swap whole structures (t = e[j]; e[j] = e[j+1]; e[j+1] = t)?
   A. To keep each employee's name, id and salary together
   B. To save memory
   C. Because salaries cannot be compared directly
   D. Because bubble sort only works on structures
   Answer: A. If only salaries were swapped, names and ids would no longer match their salaries. Swapping the whole record keeps each employee's data together. (Key point: Swapping whole structures)

2. Which call stores the sorted array in the binary file sorted.dat?
   A. fprintf() with mode "w"
   B. fwrite(e, sizeof(struct employee), n, fp) with mode "wb"
   C. fputs() with mode "a"
   D. fscanf() with mode "wb"
   Answer: B. A binary file is opened with "wb" and the array of structures is written in one call with fwrite(). (Key point: Binary file writing)

3. In this program, what does rank 1 mean?
   A. The employee with the highest salary
   B. The employee entered first
   C. The employee with the lowest salary
   D. The employee whose name comes first alphabetically
   Answer: C. Rank is the position in the file sorted in ascending order of salary, so rank 1 is the lowest salary. (Key point: Meaning of rank)

4. How does the program find the rank of the employee whose name the user gives?
   A. It counts records with fread() and compares each name using strcmp(); the counter i at the match is the rank
   B. It uses the employee id as the rank
   C. It sorts the file again by name
   D. It uses fseek() to the end of the file and counts backwards
   Answer: A. The loop reads records in sorted order, increasing i each time. When strcmp() finds the name, i is that employee's position, i.e. rank. (Key point: Finding the rank while reading)

## Related in Computer Programming

- [Sort employee structures by salary](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-sort-salary) — Computer Programming, chapter 8, asked 2×
- [Sort array ascending in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-sort-ascending) — Computer Programming, chapter 11, asked 2×
- [Display records sorted by name from file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-sorted-records-by-name) — Computer Programming, chapter 10
- [Read records until end; show nth record](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-worker-nth-record) — Computer Programming, chapter 10, asked 2×
- [Text file versus binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Atext-vs-binary-file) — Computer Programming, chapter 10
- [Store 48 students in file with append](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-48-students-append) — Computer Programming, chapter 10

## Related in other subjects

- [File stream; student binary file program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afile-stream-student-binary) — Object Oriented Programming, chapter 8
- [Binary search for 54 in sorted data](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Abinary-search-54) — Data Structure and Algorithm, chapter 8
- [Write and display 10 employees using file](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aemployee-file-program) — Object Oriented Programming, chapter 8
- [Student file read/write and modify by roll](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-modify-roll) — Object Oriented Programming, chapter 8
