Managing I/O Console using C++
C++ Stream ClassesWhat is Stream?
- A stream is an abstraction. It is a sequence of bytes.
- It represents a device on which input and output operations are performed.
- It can be represented as a source or destination of characters of indefinite length.
- It is generally associated to a physical source or destination of characters like a disk file, keyboard or console.
- C++ provides standard iostream library to operate with streams.
- The iostream is an object-oriented library which provides Input/Output functionality using streams.
In the above figure, ios
is the base class. The iostream
class is derived from istream
classes. The ifstream
are derived from istream
, respectively. These classes handles input and output with the disk files.
header file contains a declaration of ifstream, ofstream
classes. The iostream.h
file contains istream, ostream
classes and included in the program while doing disk I/O operations.
class contains input and output operations with files. The streambuf
class does not organize streams for input and output operations, only derived classes of streambuf
performs I/O operations. These derived classes arranges a space for keeping input data and for sending output data.
invokes the filebuf
functions to perform the insertion or extraction on the streams.
Advantages of Stream Classes
|istream||Input Stream||It reads and interprets input.|
|ostream||Output stream||It can write sequences of characters and represents other kinds of data.|
|ifstream||Input File Stream||The ifstream class is derived from fstreambase and istream by multiple inheritance.|
This class accesses the member functions such as get(), getline(), seekg(), tellg() and read().
It provides open() function with the default input mode and allows input operations.
|ofstream||Output File Stream||The ofstream class is derived from fstreambase and ostream classes.|
This class accesses the member functions such as put(), seekp(), write() and tellp().
It provides the member function open() with the default output mode.
|fstream||File Stream||The fstream allows input and output operations simultaneous on a filebuf. |
It invokes the member function istream::getline() to read characters from the file.
This class provides the open() function with the default input mode.
|fstreambase||File Stream Base||It acts as a base class for fstream, ifstream and ofstream. The open() and close() functions are defined in fstreambase.|
- Stream classes have good error handling capabilities.
- These classes work as an abstraction for the user that means the internal operation is encapsulated from the user.
- These classes are buffered and do not uses the memory disk space.
- These classes have various functions that make reading or writing a sequence of bytes easy for the programmer.