cout重定向到文件
原创
©著作权归作者所有:来自51CTO博客作者wx5fc8832a61484的原创作品,请联系作者获取转载授权,否则将追究法律责任
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ofstream out("out.txt");
ofstream err("err.txt");
ofstream log("log.txt");
cout.rdbuf(out.rdbuf());//重定向cout 到 out.txt
cerr.rdbuf(err.rdbuf());//重定向cerr 到 err.txt
clog.rdbuf(log.rdbuf());//重定向clog 到 log.txt
cout<<"cout test"<<endl;
cerr<<"cerr test"<<endl;
clog<<"clog test"<<endl;
return 0;
}
//Use cout for the standard output. buffered
//Use cerr to show errors. unbuffered
//Use clog for logging. buffered
cin也可以重新到文件,从文件输入。