65,189
社区成员




#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ofstream outf("out.txt");
streambuf* x = cout.rdbuf(outf.rdbuf());// redirect to out.txt
cout << "Testn"<<endl; // write to out.txt
cout.rdbuf(x); // recovery
cout << "Test2n"<<endl; // write to screen
return 0;
}
ofstream file("rdbuf.txt");
streambuf *x = cout.rdbuf(file.rdbuf());
cout << "test"<<endl;
cout.rdbuf(x);
cout<<"test2"<<endl;
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ofstream outf("out.txt");
streambuf* backup = cout.rdbuf();
cout.rdbuf(outf.rdbuf());
cout << "Testn" << endl;
cout << "Test2n" << endl;
cout << "Test3n" << endl;
cout << "Test4n" << endl;
cout << "Test5n" << endl;
cout.rdbuf(backup);
outf.close();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ofstream outf("out.txt");
streambuf* x = cout.rdbuf(outf.rdbuf());// redirect to out.txt
cout << "Testn"<<endl; // write to out.txt
// cout.rdbuf(x); // recovery
cout << "Test2n"<<endl;
cout << "Test3n"<<endl;
cout << "Test4n"<<endl;
cout << "Test5n"<<endl;
//Test2n~Test5n尽管也打印到out.txt中了,但是执行完之前报了个“段错误 (核心已转储)”的错误。。
return 0;
}