【发布时间】:2014-10-24 02:31:57
【问题描述】:
我正在尝试将输入流与文件流绑定,我希望从输入流中输入一些内容,然后自动刷新到文件流
它不起作用...我从键盘输入了一些东西,outfile 仍然是空的
#include <iostream>
#include <fstream>
#include <stdexcept>
using namespace std;
int main(int argc, char const *argv[])
{
ofstream outfile("outfile" , ofstream::app | ofstream::out);
if(!outfile)
throw runtime_error("Open the file error");
ostream * old_tie = cin.tie();//get old tie
cin.tie(0);//unbind from old tie
cin.tie(&outfile);//bind new ostream
string temp;
while(cin >> temp)
{
if(temp == ".")//stop input
break;
}
cin.tie(0);
cin.tie(old_tie);// recovery old tie
return 0;
}
【问题讨论】:
-
你真正想在这里做什么?重新分配
cin缓冲区?因为这不是tie管理的。