dupplicating std::ofstream appended content
I am using a std::ofstream for trace output.
For some reasons, I sometimes want to dupplicate what I have appended at the end of the std::ofstream (that is not flushed or closed yet), into another std::ofstream;
Do you think 开发者_StackOverflow中文版of any way of achieving this ?
Thx
The Tee filter from Boost.Iostreams can split an output stream into two.
Here's an example inspired heavily by the one given by Johannes Schaub in his answer here.
#include <sstream>
#include <iostream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/tee.hpp>
int main()
{
namespace io = boost::iostreams;
typedef io::tee_device<std::ostream, std::stringstream> TeeDevice;
typedef io::stream<TeeDevice> TeeStream;
std::stringstream ss;
TeeDevice teeDevice(std::cout, ss);
TeeStream tee(teeDevice);
tee << "Hello World\n" << std::flush;
std::cout << "ss: " << ss.str() << "\n";
}
When I omit the flush manipulator, ss.str()
returns an empty string. I don't know whether or not this is the expected behaviour.
精彩评论