Tying Streams

Tying Streams

It is common practice for interactive programs to prompt for input on one stream (such as cout) and then wait for input on another stream (such as cin).

It is possible to tie an output stream to an input stream to ensure that any prompt issued on the output stream is displayed before the request for input is made on the input stream

Stream cout is automatically tied to cin.  However, you may wish to tie two different streams together to cause them to be associated in this way.

The tie Member Function

Use the tie member function to do this:

myInputStream.tie( &myOutputStream );

It ties the two streams together.

The tie member function also returns the previously tied stream.  There is also a tie member function that only returns the tied stream:

myInputStream.tie();

To untie a stream, use:

myInputStream.tie(0); // pass in 0
Index