In C, we could use the function freopen() to redirect an existing FILE pointer to another stream. The prototype for freopen() is given as
FILE * freopen ( const char * filename, const char * mode, FILE * stream );
For Example to redirect the stdout to say a textfile, we could write
freopen ("text_file.txt", "w", stdout);
While this method is still supported in C++, this article discusses another way to redirect I/O streams.
C++ being an object-oriented programming language gives us the ability to not only define our own streams but also redirect standard streams. Thus in C++, a stream is an object whose behavior is defined by a class. Thus anything that behaves like a stream is also a stream.
Streams Objects in C++ are mainly of three types :
- istream : Stream object of this type can only perform input operations from the stream
- ostream : These objects can only be used for output operations.
- iostream : Can be used for both input and output operations
All these classes, as well as file stream classes, derive from the classes: ios and streambuf. Thus filestream and IO stream objects behave similarly.
All stream objects also have an associated data member of class streambuf. Simply put streambuf object is the buffer for the stream. When we read data from a stream, we don’t read it directly from the source, but instead, we read it from the buffer which is linked to the source. Similarly, output operations are first performed on the buffer, and then the buffer is flushed (written to the physical device) when needed.
C++ allows us to set the stream buffer for any stream. So the task of redirecting the stream simply reduces to changing the stream buffer associated with the stream. Thus the to redirect a Stream A to Stream B we need to do
- Get the stream buffer of A and store it somewhere
- Set the stream buffer of A to the stream buffer of B
- If needed reset the stream buffer of A to its previous stream buffer
We can use the function ios::rdbuf() to perform two opeations.
1) stream_object.rdbuf(): Returns pointer to the stream buffer of stream_object 2) stream_object.rdbuf(streambuf * p): Sets the stream buffer to the object pointed by p
Here is an example program below to show the steps
This line is written to screen Contents of file cout.txt: This line written to file
The above steps can be condensed into a single step
auto cout_buf = cout.rdbuf(file.rdbuf()) // sets couts streambuffer and returns the old streambuffer back to cout_buf
- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Passing and Returning Objects in C++
- Train a Support Vector Machine to recognize facial features in C++
- unordered_set operators in C++ STL
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- How to delete a range of values from the List using Iterator
- How to create a List with Constructor in C++ STL
- How to delete last element from a List in C++ STL
- Can C++ reference member be declared without being initialized with declaration?
- Finding Floor and Ceil of a Sorted Array using C++ STL
- Some useful C++ tricks for beginners in Competitive Programming
- Replace each node with its Surpasser Count in Linked List
- Comparison of Java with other programming languages
- Print multiples of Unit Digit of Given Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.