---
title: "Display records sorted by name from file"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-sorted-records-by-name"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-sorted-records-by-name
site: BCT Engineering
---

# Display records sorted by name from file

Computer Programming · Chapter 10: Data Files

- Asked in: 2070 Chaitra, 4 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-sorted-records-by-name
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Write the program to display the records in sorted order, sorting is performed in ascending order with respect to name using data files concept.

## Short answer

Read name and roll from records.txt using fscanf into a struct array. Sort ascending by name with bubble sort using strcmp (swap whole struct when out of order). Print the sorted list.

## Answer

**Logic.** The records (name and roll) are stored in a text file `records.txt`, one record per line. The program reads them into an array of structures with `fscanf()`, sorts the array in ascending order of name with `strcmp()` (bubble sort, swapping whole structures) and displays them.

```c
#include <stdio.h>
#include <string.h>
#define MAX 100

struct rec {
    char name[30];
    int roll;
};

int main(void)
{
    struct rec r[MAX], t;
    int n = 0;
    FILE *fp = fopen("records.txt", "r");

    if (fp == NULL) {
        printf("Cannot open records.txt\n");
        return 1;
    }
    while (n < MAX && fscanf(fp, "%29s %d", r[n].name, &r[n].roll) == 2)
        n++;
    fclose(fp);

    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - 1 - i; j++)
            if (strcmp(r[j].name, r[j + 1].name) > 0) {
                t = r[j]; r[j] = r[j + 1]; r[j + 1] = t;
            }

    printf("%-10s %s\n", "Name", "Roll");
    for (int i = 0; i < n; i++)
        printf("%-10s %d\n", r[i].name, r[i].roll);
    return 0;
}
```

**Output** (records.txt had the lines: Ram 1, Sita 2, Bibek 3, Hari 4, Anil 5)

```text
Name       Roll
Anil       5
Bibek      3
Hari       4
Ram        1
Sita       2
```

## Check yourself

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

1. Why is strcmp(r[j].name, r[j+1].name) > 0 used instead of r[j].name > r[j+1].name?
   A. Strings (character arrays) cannot be compared with > ; strcmp() compares their contents
   B. strcmp() is faster than >
   C. > only works for integers up to 100
   D. strcmp() returns 1 for equal strings
   Answer: A. Comparing arrays with > compares addresses, not text. strcmp() returns a positive value when the first string comes later alphabetically. (Key point: Comparing names)

2. Why is a whole structure swapped using t = r[j]; r[j] = r[j+1]; r[j+1] = t;?
   A. So the name and roll stay together
   B. Because names cannot be copied one by one
   C. To avoid reading the file again
   D. To reverse the roll numbers
   Answer: A. Each name has a roll number belonging to it; swapping the entire record keeps them paired. (Key point: Swapping records)

3. How are the records loaded from records.txt into the array?
   A. With fscanf() in a loop that continues while it returns 2 and n < MAX
   B. With fwrite() until n is 0
   C. With printf() for each line
   D. With getchar() once
   Answer: A. Each record has two fields (name and roll), so fscanf() returns 2 for a good read; the loop stops at end of file. (Key point: Loading from the file)

## Related in Computer Programming

- [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×
- [Employee text file; sort into binary file](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-employee-sorted-binary) — Computer Programming, chapter 10
- [I/O functions used with data files](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-io-functions) — Computer Programming, chapter 10, asked 2×
- [College records with nested date structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-college-estdate) — Computer Programming, chapter 8
- [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×
- [Student records with two functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-student-read-display-functions) — Computer Programming, chapter 8

## Related in other subjects

- [Student records file with CRN search](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-crn-file-search) — Object Oriented Programming, chapter 8
- [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
- [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 displayed in descending marks order](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-file-descending) — Object Oriented Programming, chapter 8
