---
title: "Read records until end; show nth record"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-worker-nth-record"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-worker-nth-record
site: BCT Engineering
---

# Read records until end; show nth record

Computer Programming · Chapter 10: Data Files

- Asked in: 2071 Chaitra, 7 marks; 2070 Asar, 8 marks (asked 2 times)
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/file-worker-nth-record
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Write a program to continuously read name, age and salary of a worker and write it into a file until user confirms to end. Then read n from user and display the nth record in the file. Details of worker must be represented by a structure. (Variant: details of book authors with name, nationality and number of books published.)

## Short answer

Define a struct for worker details. Write records to a binary file using fwrite until user stops. To read the nth record, use fseek to (n-1)*sizeof(struct) and fread. Validate n against record count.

## Answer

**Design.** The worker's details are held in `struct worker` (name, age, salary). Records are written to a binary file `worker.dat` with `fwrite()` until the user answers `n` to "Add another record?". Because every record has the same size, the n-th record starts at byte (n − 1) × sizeof(struct worker). `fseek()` jumps there and `fread()` reads it. A check on n prevents reading outside the file.

(For the variant, replace the members by author name, nationality and number of books.)

```c
#include <stdio.h>

struct worker {
    char name[30];
    int age;
    float salary;
};

int main(void)
{
    FILE *fp;
    struct worker w;
    char more;
    int n, count = 0;

    fp = fopen("worker.dat", "wb");
    if (fp == NULL) {
        printf("Cannot open file\n");
        return 1;
    }
    do {
        printf("Enter name age salary: ");
        scanf("%29s %d %f", w.name, &w.age, &w.salary);
        fwrite(&w, sizeof(w), 1, fp);
        count++;
        printf("Add another record (y/n)? ");
        scanf(" %c", &more);
    } while (more == 'y' || more == 'Y');
    fclose(fp);

    printf("Enter n: ");
    scanf("%d", &n);
    if (n < 1 || n > count) {
        printf("No such record (file has %d records)\n", count);
        return 1;
    }
    fp = fopen("worker.dat", "rb");
    if (fp == NULL) {
        printf("Cannot open file\n");
        return 1;
    }
    fseek(fp, (long)(n - 1) * sizeof(w), SEEK_SET);   /* jump to nth record */
    fread(&w, sizeof(w), 1, fp);
    printf("Record %d: %s %d %.2f\n", n, w.name, w.age, w.salary);
    fclose(fp);
    return 0;
}
```

**Sample run** (three workers entered, then n = 2)

Input: `Ram 30 12000 y Sita 25 15000 y Hari 41 9000 n 2`

```text
Enter name age salary: Add another record (y/n)? Enter name age salary: Add another record (y/n)? Enter name age salary: Add another record (y/n)? Enter n: Record 2: Sita 25 15000.00
```

## Check yourself

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

1. Why are the worker records written with fwrite into a binary file?
   A. Every record has the same size, so the nth can be located directly
   B. Binary files are readable in an editor
   C. fwrite converts to text
   D. fprintf cannot be used in C
   Answer: A. Fixed-size records make direct positioning possible. (Key point: Binary file for records)

2. At which byte offset does the nth record start?
   A. n * sizeof(struct worker)
   B. (n - 1) * sizeof(struct worker)
   C. n + sizeof(struct worker)
   D. sizeof(struct worker) / n
   Answer: B. The first record starts at 0, so record n starts after n-1 records. (Key point: Offset of nth record)

3. Which function moves to the nth record before fread?
   A. rewind()
   B. ftell()
   C. fseek()
   D. fclose()
   Answer: C. fseek(fp, offset, SEEK_SET) moves the file position to the given offset. (Key point: Jumping to the record)

4. Why is n checked against the record count?
   A. To prevent reading outside the file
   B. To make fseek faster
   C. To change the mode
   D. To close the file
   Answer: A. A value outside 1..count would read nonexistent data. (Key point: Validating n)

## Related in Computer Programming

- [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
- [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×
- [College records with nested date structure](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-college-estdate) — Computer Programming, chapter 8
- [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
- [nth Fibonacci term prime check](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afibonacci-nth-prime) — Computer Programming, chapter 6
- [Employees with salary above 10,000](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astruct-employee-salary-above-10000) — 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
- [Paired test: safety program worker hours](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Apaired-before-after-safety-program) — Probability and Statistics, chapter 6, asked 3×
