---
title: "Stream and file stream classes"
subject: "Object Oriented Programming"
chapter: 8
key: "semester_3/object_oriented_programming:stream-file-classes"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/stream-file-classes
site: BCT Engineering
---

# Stream and file stream classes

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

- Asked in: 2068 Baisakh, 2+2 marks
- Video: [Reading and Writing to Files (ifstream and ofstream) - C++ Tutorial 25](https://www.youtube.com/watch?v=Cz4fl-TUjVk) — Caleb Curry
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/stream-file-classes
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-8

## Question

What do you mean by stream? Explain different stream class for file input/output.

## Short answer

A stream is a sequence of bytes flowing between program and I/O device. For file I/O, ifstream reads, ofstream writes, fstream does both, and filebuf manages the buffer; all derived from ios offer open(), close() and eof().

## Answer

#### Stream
A **stream** is a sequence of bytes that flows between a program and an I/O device (keyboard, monitor, file). Input streams carry data into the program and output streams carry data out. C++ treats all devices uniformly as streams, so the same operators `>>` and `<<` work with the console and with files.

#### Stream classes for file I/O
Declared in `<fstream>`:

| Class | Base class | Purpose |
|---|---|---|
| `ifstream` | `istream` | Reads from a file (input); members `open()`, `get()`, `getline()`, `read()`, `seekg()`, `tellg()` |
| `ofstream` | `ostream` | Writes to a file (output); members `open()`, `put()`, `write()`, `seekp()`, `tellp()` |
| `fstream` | `iostream` | Reads and writes the same file |
| `filebuf` | `streambuf` | Manages the file buffer and the actual transfer of data |

`ifstream`, `ofstream` and `fstream` are derived from `ios` (through `istream`/`ostream`), so they share the formatting and error-state functions. Functions common to all three are `open()`, `close()`, `is_open()` and `eof()`. `filebuf` is not derived from `ios`; it is the buffer that each file stream uses internally.

```cpp
ofstream out("a.txt");   // output file
out << "Hello";
out.close();
ifstream in("a.txt");    // input file
```

## Check yourself

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

1. What is a stream in C++?
   A. A type of loop
   B. A special kind of array
   C. A sequence of bytes flowing between a program and an I/O device
   D. A file stored on disk only
   Answer: C. A stream is a flow of bytes between the program and a device such as keyboard, monitor or file, so the same >> and << work for all. (Key point: Meaning of stream)

2. Which class is used only to write to a file, and which base does it come from?
   A. ifstream from istream
   B. ofstream from ostream
   C. fstream from streambuf
   D. filebuf from ios
   Answer: B. ofstream derives from ostream and provides output members like put(), write() and seekp(). (Key point: Class for writing)

3. When is fstream used?
   A. Only to read a file
   B. Only to manage the buffer
   C. Only for console I/O
   D. To read and write the same file
   Answer: D. fstream derives from iostream and supports both input and output on the same file. (Key point: fstream use)

4. What does filebuf do?
   A. Reads keyboard input
   B. Manages the file buffer and the actual data transfer
   C. Reports error flags
   D. Defines the << operator
   Answer: B. filebuf (derived from streambuf) handles buffering and the actual transfer of data to the file. (Key point: Role of filebuf)

## 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
- [Stream class hierarchy](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astream-class-hierarchy) — Object Oriented Programming, chapter 8, asked 4×
- [Add objects of two different classes](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-add-two-class-objects) — Object Oriented Programming, chapter 4, asked 2×
- [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
- [Time class: +, <= and << overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Atime-operator-overload) — Object Oriented Programming, chapter 5
- [Overload index operator [] for array class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aindex-operator-array-class) — Object Oriented Programming, chapter 5

## Related in other subjects

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Output of e^(−2t)u(t) to a rectangular input](https://www.bctengineer.com/question.md?key=semester_4%2Fsignals_and_systems%3Aconvolution-exponential-rect-input) — Signals and Systems, chapter 5, asked 4×
- [CE configuration input/output characteristics](https://www.bctengineer.com/question.md?key=semester_2%2Fbasic_electronics_engineering%3Ace-npn-characteristics) — Basic Electronics Engineering, chapter 3, asked 3×
- [LTI output for h(t)=e^(-2t), rectangular input](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Alti-output-rect-input) — Data Communication, chapter 2, asked 3×
