---
title: "Student records file with CRN search"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:student-crn-file-search"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/student-crn-file-search
site: BCT Engineering
---

# Student records file with CRN search

Object Oriented Programming · Chapter 8: Stream Computation for Console and File Input/Output

- Asked in: 2073 Shrawan, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/student-crn-file-search
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

Create class student to store Name, Age and CRN of students. Write a program to write records of N numbers of students into the file. And your program should search complete information of students from file according to CRN entered by user and display it.

## Short answer

Write N student records to stud.dat in binary mode. To search by CRN, read records sequentially until a matching CRN is found and display details; if not found, show a message.

## Answer

Assumption: records are saved in the binary file `stud.dat`; the search is a sequential read comparing CRN.

```cpp
#include <iostream>
#include <fstream>
using namespace std;

class Student {
    char name[30];
    int age;
    long crn;
public:
    void getData() {
        cout << "Name, Age, CRN: ";
        cin >> name >> age >> crn;
    }
    void display() {
        cout << "Name: " << name << "\nAge: " << age
             << "\nCRN: " << crn << endl;
    }
    long getCrn() { return crn; }
};

int main() {
    Student s;
    int n;
    cout << "Number of students: ";
    cin >> n;

    ofstream fout("stud.dat", ios::binary);
    for (int i = 0; i < n; i++) {
        s.getData();
        fout.write((char*)&s, sizeof(s));
    }
    fout.close();

    long key;
    cout << "Enter CRN to search: ";
    cin >> key;

    ifstream fin("stud.dat", ios::binary);
    bool found = false;
    while (fin.read((char*)&s, sizeof(s))) {
        if (s.getCrn() == key) {
            s.display();
            found = true;
            break;
        }
    }
    fin.close();
    if (!found) cout << "No student with CRN " << key << endl;
    return 0;
}
```

## Check yourself

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

1. How does the program store each Student record in stud.dat?
   A. fout << s for each member as text
   B. fout.write((char*)&s, sizeof(s)) in binary mode
   C. fout.put(s) one character at a time
   D. fout.seekp(s) to the CRN position
   Answer: B. The object is written as raw bytes with write() on a stream opened with ios::binary, so every record has the same size. (Key point: Binary write of an object)

2. How does the program find the student whose CRN the user entered?
   A. Jumps directly to byte position CRN x sizeof(Student)
   B. Reads records one by one and compares getCrn() with the key
   C. Sorts the file first and then uses binary search
   D. Uses the eof() function to detect a matching CRN
   Answer: B. It reads each record in a loop with read() and compares s.getCrn() == key, stopping with break when found. (Key point: Sequential search by CRN)

3. Why does the class provide the member function getCrn()?
   A. Because crn is private and main() must compare it with the key
   B. To write the CRN to the file
   C. To make the class binary-compatible
   D. To convert CRN to a string
   Answer: A. crn is a private member, so main() can only read it through a public member function. (Key point: Why a getter is needed)

4. What is the purpose of the bool variable found?
   A. To stop the file from being opened twice
   B. To tell after the loop whether to print 'No student' because no CRN matched
   C. To store the number of records read
   D. To check whether the file was written correctly
   Answer: B. found is set true on a match; if it is still false after the loop, the no-student message is printed. (Key point: Not-found handling)

## Related in Object Oriented Programming

- [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
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [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
- [Patient information store and retrieve](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apatient-file-program) — Object Oriented Programming, chapter 8
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6

## Related in other subjects

- [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
- [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
- [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
- [Binary file of 48 students; highest mark](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afile-binary-48-students-highest) — Computer Programming, chapter 10
