I/O Redirection in C++

 I/O Redirection

In this section, we will learn about the concept of input/output redirection in C++ using the streambuf and ios classes.The input and output in C++ are performed in a sequence of bytes referred to as a stream. There are multiple libraries in C++ which helps us to use the basic input/output functions of c++.

I/O Redirection in C++

Input/Output Redirection in C++

In C++ programming language, the concept of Input and Output Redirection can be explained as follows with the help of given stream objects.

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, derived 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.

Two operations using ios::rdbuf() :

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

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.

To redirect a Stream A to Stream B we need to do:-

  1. Get the stream buffer of A and store it somewhere
  2. Set the stream buffer of A to the stream buffer of B
  3. If needed to reset the stream buffer of A to its previous stream buffer

Implementation of I/O Redirection in C++


Example :

using namespace std;
int main ()
  fstream file;
  file.open ("cout.txt", ios::out);
  string line;
  streambuf *stream_buffer_cout = cout.rdbuf ();
  streambuf *stream_buffer_cin = cin.rdbuf ();
  streambuf *stream_buffer_file = file.rdbuf ();
  cout.rdbuf (stream_buffer_file);
  cout << "This line written to file" << endl;
  cout.rdbuf (stream_buffer_cout);
  cout << "This line is written to screen" << endl;
  file.close ();
  return 0;


This line is written to screen
Contents of file cout.txt:
This line written to file

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription