---
title: "File stream; student binary file program"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:file-stream-student-binary"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/file-stream-student-binary
site: BCT Engineering
---

# File stream; student binary file program

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

- Asked in: 2068 Chaitra, 2+8 marks
- Video: [Using fstream. Writing into a Binary file in C++. Data File Handling](https://www.youtube.com/watch?v=qdrfhzyqnbM) — Anjali Luthra
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/file-stream-student-binary
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

What is a file stream? Write a class student with roll, name, address, marks as member variables. Use a member function to write records of students in a binary file and another member function to read records from file. Write a program to search a specific record of student using roll number as key from user input.

## Short answer

A file stream connects a program to a disk file. The Student class uses ofstream (binary, append) to write the object directly, and ifstream (binary) to read records. To search by roll, loop reading records; if roll matches, display the record.

## Answer

#### File stream
A **file stream** is a stream that connects a program to a disk file so that data can be read from or written to it using the normal I/O operators and functions. C++ provides `ifstream` (input), `ofstream` (output) and `fstream` (both) in `<fstream>`.

#### Program

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

class Student {
    int roll;
    char name[30], address[30];
    float marks;
public:
    void getData() {
        cout << "Roll, Name, Address, Marks: ";
        cin >> roll >> name >> address >> marks;
    }
    void display() {
        cout << roll << "\t" << name << "\t" << address
             << "\t" << marks << endl;
    }
    int getRoll() { return roll; }

    void writeRecord() {                       // write one record
        ofstream fout("student.dat", ios::binary | ios::app);
        fout.write((char*)this, sizeof(*this));
        fout.close();
    }
    void readRecords() {                       // read all records
        ifstream fin("student.dat", ios::binary);
        while (fin.read((char*)this, sizeof(*this)))
            display();
        fin.close();
    }
};

int main() {
    Student s;
    int n;
    cout << "How many students? ";
    cin >> n;
    for (int i = 0; i < n; i++) {
        s.getData();
        s.writeRecord();
    }

    cout << "\nAll records:\n";
    s.readRecords();

    int key;
    bool found = false;
    cout << "\nEnter roll number to search: ";
    cin >> key;
    ifstream fin("student.dat", ios::binary);
    while (fin.read((char*)&s, sizeof(s))) {
        if (s.getRoll() == key) {
            cout << "Record found:\n";
            s.display();
            found = true;
            break;
        }
    }
    fin.close();
    if (!found) cout << "Record not found\n";
    return 0;
}
```

Binary mode stores each object exactly as it is in memory, so every record has the same size and can be read back with `read()` in one step. Character arrays are used instead of `string` so the records contain no pointers.

## Check yourself

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

1. What is a file stream?
   A. An array of files
   B. A function that deletes files
   C. A stream connecting a program to a disk file for reading/writing
   D. A memory buffer without any file
   Answer: C. A file stream links the program to a disk file, using ifstream, ofstream or fstream. (Key point: File stream definition)

2. Why does writeRecord() open the file with ios::binary | ios::app?
   A. To read the file backwards
   B. To convert numbers to text
   C. To lock the file
   D. To add each new record at the end without erasing earlier ones
   Answer: D. ios::app appends, so each call adds a record without truncating the existing data. (Key point: Appending records)

3. Why does the class use character arrays instead of string?
   A. Records contain no pointers, so raw bytes can be saved and read back safely
   B. Because string cannot be printed
   C. To make search faster
   D. Because arrays are always smaller
   Answer: A. write() stores raw memory; a string holds a pointer to its data, so only fixed arrays give a self-contained record. (Key point: Why char arrays)

4. How is a record found by roll number?
   A. Use seekg with the roll number as bytes
   B. Read records in a loop and compare getRoll() with the key
   C. Sort by name first
   D. Compare file sizes
   Answer: B. The program reads each record and compares getRoll() with the key, displaying it when equal. (Key point: Searching by roll)

## Related in Object Oriented Programming

- [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
- [Member function as friend of another class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Amember-function-friend-of-other-class) — Object Oriented Programming, chapter 4
- [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
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [Binary operator member function one argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Abinary-member-one-argument) — Object Oriented Programming, chapter 5

## Related in other subjects

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