You can do this by creating a class that extends std::streambuf and has a member std::ofstream . After overriding the member functions std::streambuf::overflow and std::streambuf::sync everything will be installed.
Most of the code is here . The material that I added ("ADDED:") for mirroring files is indicated. It can be overly complicated since I am at work and cannot completely mess it up to simplify it, but it works - the bonus to doing it this way (instead of just using std::streambuf* is that any code (say, you have an external library) that writes to std::cout will write to your file.
mystreambuf.h
#ifndef MYSTREAMBUF_H
mystreambuf.cpp
// Based on class by perfectly.insane from http://www.dreamincode.net/code/snippet2499.htm
hope this helps; greetings
source share