C++ std::ios::tie














































C++ std::ios::tie



Description: 

get (1)
ostream* tie() const;
set (2)
ostream* tie (ostream* tiestr);
Get/set tied stream

The first form (1) returns a pointer to the tied output stream.

The second form (2) ties the object to tiestr and returns a pointer to the stream tied before the call, if any.

The tied stream is an output stream object which is flushed before each i/o operation in this stream object.


Parameters:     

tiestr
An output stream object.

 

Return Value:

A pointer to the stream object tied before the call, or a null pointer in case the stream was not tied.

 

Program:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// redefine tied object
#include <iostream>     // std::ostream, std::cout, std::cin
#include <fstream>      // std::ofstream

int main () {
  std::ostream *prevstr;
  std::ofstream ofs;
  ofs.open ("test.txt");

  std::cout << "tie example:";

  *std::cin.tie() << "This is inserted into cout";
  prevstr = std::cin.tie (&ofs);
  *std::cin.tie() << "This is inserted into the file";
  std::cin.tie (prevstr);

  ofs.close();

  return 0;
}

Output:

tie example:
This is inserted into cout
Notice that in the first set of extractions, the leading spaces were ignored, while in the second they were extracted as valid characters.

Comments