开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜