// iostatesaver.cpp : 定义控制台应用程序的入口点。
//iostatesaver状态保存机
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <boost/io/ios_state.hpp>
#include <string>
using namespace std;
void test_iossaver()
{
string strfile = "d:/iossavertest.txt";
{
ofstream of(strfile, std::ios_base::out);
//此处记录cout的状态,保证不会因为
//ofstream的声明周期结束而导致错误
boost::io::ios_all_saver allsaver(cout);
cout.set_rdbuf(of.rdbuf());
cout<<"what are you doing!"<<endl;
cout.flush();
of.close();
}
//That's OK
cout<<"will crashed!"<<endl;
cout.flush();
}
int _tmain(int argc, _TCHAR* argv[])
{
test_iossaver();
return 0;
}
//iostatesaver状态保存机
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <boost/io/ios_state.hpp>
#include <string>
using namespace std;
void test_iossaver()
{
string strfile = "d:/iossavertest.txt";
{
ofstream of(strfile, std::ios_base::out);
//此处记录cout的状态,保证不会因为
//ofstream的声明周期结束而导致错误
boost::io::ios_all_saver allsaver(cout);
cout.set_rdbuf(of.rdbuf());
cout<<"what are you doing!"<<endl;
cout.flush();
of.close();
}
//That's OK
cout<<"will crashed!"<<endl;
cout.flush();
}
int _tmain(int argc, _TCHAR* argv[])
{
test_iossaver();
return 0;
}