流可以绑定到一个输出流上,这样在每次输入/ 输出前可以刷新输出流的缓冲区
比如:cin 绑定到了cout 上
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ofstream os("test.txt");
std::ifstream is("test.txt");
std::string value("0");
os << "Hello";
is >> value;
std::cout << "Result before tie(): \"" << value << "\"\n";
is.clear();
is.tie(&os);
is >> value;
std::cout << "Result after tie(): \"" << value << "\"\n";
}
输出:
Result before tie(): “0”
Result after tie(): “Hello”