- 2068 Baisakh · 2+2 marks
Stream and file stream classes
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().
What do you mean by stream? Explain different stream class for file input/output.
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.
ofstream out("a.txt"); // output file
out << "Hello";
out.close();
ifstream in("a.txt"); // input file
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- File stream; student binary file program · Object Oriented Programming, chapter 8
- Stream class hierarchy · Object Oriented Programming, chapter 8 · asked 4×
- Add objects of two different classes · Object Oriented Programming, chapter 4 · asked 2×
- Student records file with CRN search · Object Oriented Programming, chapter 8
- Time class: +, <= and << overloading · Object Oriented Programming, chapter 5
- Overload index operator [] for array class · Object Oriented Programming, chapter 5
- Create classes from design class diagrams · Object Oriented Analysis and Design, chapter 4 · asked 2×
- Output of e^(−2t)u(t) to a rectangular input · Signals and Systems, chapter 5 · asked 4×
- CE configuration input/output characteristics · Basic Electronics Engineering, chapter 3 · asked 3×
- LTI output for h(t)=e^(-2t), rectangular input · Data Communication, chapter 2 · asked 3×