To tie an ostream to the calling stream.
basic_ostream<charT, traits>* tie() const;
basic_ostream<charT, traits>* tie
(basic_ostream<charT, traits>* tiestr);
Any stream can have an ostream tied to it to ensure that the ostream is flushed before any operation. The standard input and output objects cin and cout are tied to ensure that cout is flushed before any cin operation. The function tie() is overloaded. The parameterless version returns the current ostream that is tied, if any. The tie() function with an argument ties the new object to the ostream and returns a pointer, if any, from the first. The post-condition of tie() function that takes the argument tiestr is that tiestr is equal to tie();
A pointer to type ostream that is or previously tied, or zero if there was none.
// The file EWL-test contains // CodeWarrior "Software at Work" #include <iostream> #include <fstream> #include <cstdlib> char inFile[] = "EWL-test"; int main() { using namespace std; ifstream inOut(inFile, ios::in | ios::out); if(!inOut.is_open()) { cout << "file is not open"; exit(1);} ostream Out(inOut.rdbuf()); if(inOut.tie()) cout << "The streams are tied\n"; else cout << "The streams are not tied\n"; inOut.tie(&Out); inOut.rdbuf()->pubseekoff(0, ios::end); char str[] = "\nRegistered Trademark"; Out << str; if(inOut.tie()) cout << "The streams are tied\n"; else cout << "The streams are not tied\n"; inOut.close(); return 0; }
Result:
The streams are not tied The streams are tied The file EWL-test now contains CodeWarrior "Software at Work" Registered Trademark