我正在开发一个大量使用“cout
我正在使用 Visual Studio 2008。我已经看到了这个帖子Google 网上论坛:将 cout 重定向到文件,这似乎正是我想做的。唯一的问题是代码无法编译。我在 ->overflow() 和 ->sync() 方法调用上收到 C2248 错误“无法访问受保护的成员”。
有人知道如何编译这段代码吗?或者同时将 cout 重定向到控制台和文件的替代方法?
The boost::iostreams::tee_device就是为此而生的
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>
#include <fstream>
#include <iostream>
int
main()
{
typedef boost::iostreams::tee_device<std::ostream, std::ofstream> Tee;
typedef boost::iostreams::stream<Tee> TeeStream;
std::ofstream file( "foo.out" );
Tee tee( std::cout, file );
TeeStream both( tee );
both << "this goes to both std::cout and foo.out" << std::endl;
return 0;
}
示例调用:
samm$ ./a.out
this goes to both std::cout and foo.out
samm$ cat foo.out
this goes to both std::cout and foo.out
samm$
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)