---
title: "Binary file of 48 students; highest mark"
subject: "Computer Programming"
chapter: 10
key: "semester_1/computer_programming:file-binary-48-students-highest"
source: https://www.bctengineer.com/q/semester_1/computer_programming/file-binary-48-students-highest
site: BCT Engineering
---

# Binary file of 48 students; highest mark

Computer Programming · Chapter 10: Data Files

- Asked in: 2068 Chaitra, 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-binary-48-students-highest
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-10

## Question

Write a program to input and save record like name, roll, address and obtained mark of 48 students in a binary file and search and display the record of a student whose obtained mark in highest. The information should be organized in a structure.

## Short answer

Define struct student with name, roll, address, mark. Input 48 records, write to binary file student.dat with fwrite. Reopen for reading, read with fread, track the record with maximum mark, then display that student’s details.

## Answer

**Design.** The structure `student` holds name, roll, address and mark. All 48 records are written to the binary file `student.dat` with `fwrite()`. The file is reopened with `"rb"` and records are read one by one with `fread()`; the record with the highest mark is kept in `top` (the first record starts it). That record is displayed at the end.

```c
#include <stdio.h>
#ifndef N
#define N 48
#endif

struct student {
    char name[30];
    int roll;
    char address[30];
    float mark;
};

int main(void)
{
    FILE *fp;
    struct student s, top;
    int first = 1;

    fp = fopen("student.dat", "wb");
    if (fp == NULL) { printf("Cannot open file\n"); return 1; }
    for (int i = 1; i <= N; i++) {
        printf("Student %d (name roll address mark): ", i);
        scanf("%29s %d %29s %f", s.name, &s.roll, s.address, &s.mark);
        fwrite(&s, sizeof s, 1, fp);
    }
    fclose(fp);

    fp = fopen("student.dat", "rb");
    if (fp == NULL) { printf("Cannot open file\n"); return 1; }
    while (fread(&s, sizeof s, 1, fp) == 1)
        if (first || s.mark > top.mark) {
            top = s;
            first = 0;
        }
    fclose(fp);

    printf("Highest mark: %s, Roll %d, %s, %.1f\n", top.name, top.roll, top.address, top.mark);
    return 0;
}
```

**Sample run** (N reduced to 3 with `-DN=3` for the test; the real program uses 48)

Input: `Ram 1 Lalitpur 65 Sita 2 Kathmandu 88.5 Hari 3 Pokhara 72`

```text
Student 1 (name roll address mark): Student 2 (name roll address mark): Student 3 (name roll address mark): Highest mark: Sita, Roll 2, Kathmandu, 88.5
```

## Check yourself

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

1. How are the 48 student records saved to student.dat?
   A. fprintf() in text mode, one line each
   B. fwrite(&s, sizeof s, 1, fp) with the file opened in "wb" mode
   C. puts() for every field
   D. fscanf() with "wb" mode
   Answer: B. Binary storage uses fwrite() of the whole structure into a file opened with "wb". (Key point: Writing the records)

2. Why is the file reopened with "rb" and read in a loop with fread(...) == 1?
   A. To read each record in turn until fread() fails at end of file
   B. To read all 48 marks in one number
   C. To delete the file after reading
   D. Because "wb" cannot be closed
   Answer: A. fread() returns 1 while a record is read successfully; when it returns 0 the end of the file has been reached. (Key point: Reading the records)

3. What is the purpose of the variable first in the highest-mark search?
   A. It counts the students
   B. It makes the first record the initial top before comparing
   C. It stores the highest roll number
   D. It skips the first record
   Answer: B. top has no value at the start, so the first record is copied into it; later records replace it only if their mark is larger. (Key point: Starting the maximum)

4. Which test updates the record kept as top?
   A. s.mark < top.mark
   B. s.roll > top.roll
   C. s.mark > top.mark
   D. strcmp(s.name, top.name) > 0
   Answer: C. A record becomes the new top when its mark is greater than the current top's mark. For marks 65, 88.5, 72 the top is Sita. (Key point: Test for highest)

## Related in Computer Programming

- [Pass student array; display passed students](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-whole-array-student-info) — Computer Programming, chapter 7
- [96 students record; pass marks](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-96-students-ioe-txt) — 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
- [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
- [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
- [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

- [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
- [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
- [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
- [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
