What are stream classes in C++?
Explain the hierarchy of stream
classes.
A stream refers to a sequence of characters that are transferred between the program and
input/output (I/O) devices. Stream classes in C++ facilitate input and output operations on
files and other I/O devices. These classes have specific features to handle program input and
output, making it easier to write portable code that can be used across multiple platforms.
To use streams in C++, you need to include the appropriate header file. For instance, to use
input/output streams, you would include the iostream header file. This library provides the
necessary functions and classes to work with streams, enabling you to read and write data to
and from files and other I/O devices.
In this blog, we’ll be exploring four essential C++ stream classes:
1. istream
2. ostream
3. ifstream
4. ofstream
1. The istream
class
This class is a general-purpose input stream and is used to read input from various sources,
including the console and files. One example of an istream is cin, which is a commonly used
input stream for reading input from the console. The istream class provides a range of
functions for handling characters, strings, and objects,
including get, getline, read, ignore, putback, and cin.
These functions enable developers to manipulate input in various ways. For example, the get
function allows developers to read a single character from the input stream, while
the getline function reads an entire line of text and stores it in a string object. The read
function is used to read a specific number of characters from the input stream and store
them in a buffer. Overall, the istream class is an essential component of input stream
handling in C++.
2. The ifstream class
When working with the ifstream class in your code, you may come across situations where
you need to read data from a file to proceed with your program. This is where file handling
comes into play, and it involves using stream classes to accomplish this task.
An ifstream object represents an input file stream, which is used to read data from a file.
Since an ifstream is a type of istream, any operations that can be performed on
an istream can also be performed on an ifstream.
One common example of an istream is cin, which is used for standard input. Therefore, any
operations that you can perform with cin can also be performed with an ifstream object.
To use ifstream (and ofstream) in your code, you need to include the fstream header by
adding the following line at the beginning of your program:
#include <fstream>
3. The ostream class
The ostream class is responsible for working with the output stream and provides all the
necessary functions for managing chars, strings, and objects such as put, write, cout etc.
4. The ofstream class
An ofstream is an output file stream, and works exactly like ifstreams, except for output
instead of input.
Once an ofstream is created, opened, and checked for no failures, you use it just like cout:
Difference Between istream, ostream, and iostream in C++ :
Feature istream ostream iostream
Full Form Input Stream Output Stream Input Output Stream
Used for input Used for output Used for both input and
Purpose
operations operations output operations
Derived from both
Derived From ios ios
istream and ostream
Direction of Data flows into Data flows out of
Supports both directions
Data Flow the program the program
Common Combination of cin and
cin cout
Object cout features
get(), getline(), Uses functions of both
Main Functions put(), write()
read() classes
>> (Extraction << (Insertion
Operator Used Uses both >> and <<
Operator) Operator)
Reading data from Displaying data on Reading and writing
Example Use
keyboard screen together
Example of istream
#include<iostream>
using namespace std;
int main() {
int x;
cin >> x; // Input operation
return 0;
}
Here, cin is an object of istream.
Example of ostream
#include<iostream>
using namespace std;
int main() {
int x = 10;
cout << x; // Output operation
return 0;
}
Here, cout is an object of ostream.
Example of iostream
#include<iostream>
using namespace std;
int main() {
int x;
cin >> x; // Input
cout << x; // Output
return 0;
}
iostream supports both input and output operations.