Stream Classes in C++
Introduction
For developing robust and adaptable applications, C++ is the programming language of choice for many developers. From operating systems to video games, the programming language C++ is the go-to language for delivering solutions that perform well in various industries.
The support for streams that C++ comes with out of the box is one of its unique features. Information in and out of your applications can be easily managed with C++. C++ streams are vital for keeping your code in the flow, whether pulling data to cout or from cin.
In this tutorial, we will learn more about the expanse of C++ streams.
What are C++ Streams
An exchange of characters among a program's memory and input/output (I/O) devices is a stream in the C++ programming language. C++'s stream classes make it easier to send and receive data to and from documents and other I/O devices. It is simpler to create portable code that can be utilized across various platforms because these types of classes have particular characteristics for handling program input and output.
You have to include the correct header file to use streams in C++. For using input/output streams, for instance, you'd need to include an iostream header file. You can read and write data from and to documents and other I/O devices using the built-in functions and classes provided by this library when utilizing streams.
There are four types of Stream Classes:
- is stream
- os stream
- if steam
- of stream
The concept of inheritance is central to Object-Based programming languages like C++. A class can inherit characteristics from a different class that was previously defined thanks to inheritance. Then, descendants can enhance their parent class by including their unique characteristics.
As an illustration, consider the hierarchy of stream classes. We can observe that ifstream represents a specialization of istream in the picture below (only a portion of it is shown). This indicates that an ifstream object is an istream object and inherits all the characteristics of the istream class's properties. It also adds a few extra characteristics of its own.
The C++ stream class hierarchy comprises various classes that specify and offer various flows for class objects. The structure of the hierarchy is set up so that the ios class, which occupies the top, is followed by classes like istream, ostream, istream, iostream, istream_withassign, and ostream_withassign.
- Both the istream and ostream classes acquire their parent class in the hierarchy, which is the ios class.
- Combining these two classes creates the iOS class, which makes up the top tier of the C++ stream class hierarchy.
- The _withassign classes, among the others in the hierarchy, offer operations for various operations, including assignment operations.
1. The istream class
This class, which is an all-purpose input stream, is employed to read data from a variety of sources, such as files and the console.
- The input stream known as cin, which is frequently used to read input on a console, is an illustration of an istream.
- Several functions, including get, getline, read, ignore, putback, and cin, are available for handling characters, strings, and objects in the istream class.
- Programmers can alter the input in many different ways thanks to those functions.
- This class is the base class for all stream classes.
- The streams can be input or output streams. This class defines members independently of how the templates of the class are defined.
For instance, whereas the getline function examines a whole line of text and retains it in a string object, the get function only enables builders to retrieve just one character compared to the input stream.
Example
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World";
}
Output
![Stream Classes in C++](https://static.tutorialandexample.com/cpp/stream-classes-in-cpp1.png)
2.The ifstream Class
There may be times when using the ifstream class in your code that you have to read data from a file in order to continue running your application. To accomplish this task, stream classes are used in file handling, which is where they come into play.
The input file streams are utilized to read data from files, and an ifstream object reflects one of these streams. Since an ifstream is a type that is a subtype of an istream, all operations that are valid for an istream are valid for an ifstream as well.
- The standard input stream cin is a typical illustration of an istream. As a result, you can use an ifstream object to carry out any actions that you can carry out with cin.
- You must contain the fstream header in your code before using ifstream (or ofstream), so add this line at the start of your program:
#include <fstream>
3. The osstream class
The ostream class, which manages the output stream and offers every one of the necessary operations for handling characters, strings, and objects like put, write, cout, etc., is in charge of working with the output stream.
You must add the line that follows at the start of your program to include the osstream header if you want to use osstream (and osstream) in the code you write:
#include<osstream>
Example
#include <iostream>
using namespace std;
int main()
{
char u;
cin.get(u);
cout.put(u);
}
Output
![Stream Classes in C++](https://static.tutorialandexample.com/cpp/stream-classes-in-cpp2.png)
3.The ofstream class
When compared to ifstreams, an ofstream is a file containing the output stream that operates precisely the same way but handles output rather than input.
An ofstream is utilized in the same way as a cout after it has been created, opened, and examined for errors.
Example
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
ofstream file1("file.dat");
file1 << "The content present in the file are: Welcome to C++ " << endl;
file1.close();
string read1;
ifstream file2("file.dat");
cout << "The contenet in the file : " << endl;
while (getline (file2, read1))
{
cout << read1;
}
}
Output
![Stream Classes in C++](https://static.tutorialandexample.com/cpp/stream-classes-in-cpp3.png)
Explanation
- The header file fstream has been imported into the program mentioned above to allow us to use stream and ifstream.
- To execute cout and cin in the program, a second header file called iostream is imported next.
- Then a standard namespace is used as the default. Then the file with the name file is accessed, and data of the data type stream is written into the file.
- Next, the contents are written into a file using a variable with the ofstream data type. The copy of the file that was opened to receive the contents is then closed using the ofstream type of data variable previously used to open the file.
- After that, a string variable is created. Following that, the ifstream type of information is used to open the file with the name file and examine its contents. The file's contents are then read and displayed as results using a variable of the ifstream data type. A file that was opened to read the file's contents is then closed using the variable of the ifstream data type.
Example 2
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
ofstream file1("file1.dat");
file1 << "The contents present in the file are: Important of stream classes in C++" << endl;
file1.close();
string read1;
ifstream file2("file1.dat");
cout << "The contents present in the file are : " << endl;
while (getline (filename2, read1))
{
cout << read1;
filename2.close();
}
}
Output
![Stream Classes in C++](https://static.tutorialandexample.com/cpp/stream-classes-in-cpp4.png)
Explanation
- The header file fstream has been included in the program mentioned above so that ofstream and ifstream can be used. Then we import an additional header file called iostream so that we can use cout as well as cin in the program.
- After that, the standard namespace is utilized. A file named filename is subsequently accessed, and its contents are written into it utilizing the ofstream data type.
- The information within it is then written into the file using the ofstream type of data variable. After that, the document that was opened to receive the contents is closed using the variable ofstream data type. After that, the string variable is created.
- The contents of the file are retrieved and displayed as output using a variable of the ifstream data type. A file that was opened in order to read the contents of the file is then closed through the variable of the ifstream data type.
Advantages of using stream classes in C++
While streams are used for implementing input and output operations in C and C++, the C++ I/O stream classes offer exactly the same input and output capabilities as C stdio.h. The Standard C++ Library's I/O stream classes have the following benefits:
- Both the output () and input (>>) operators are typesafe. When compared with scanf() and printf(), these functions are simpler to use.
- For defining both input and output for different types and classes, you can overload both the input and output operators. This ensures consistency in input and output for all types, including your own.
Conclusion
A robust feature of the C++ programming language, C++ streams offer an established framework for handling input and output. There are many different sources of data you can read from and write to, such as files, input and output devices, and other programs. You can write more effective, platform-neutral code by comprehending its syntax and functionality and using it.