I have a requirement, I need to use printf
and cout
to display the data into console and file
as well.
For printf
I have
You can swap the underlying buffers. Here is that done facilitated through RAII.
#include
class buffer_restore
{
std::ostream& os;
std::streambuf* buf;
public:
buffer_restore(std::ostream& os) : os(os), buf(os.rdbuf())
{ }
~buffer_restore()
{
os.rdbuf(buf);
}
};
int main()
{
buffer_restore b(std::cout);
std::ofstream file("file.txt");
std::cout.rdbuf(file.rdbuf());
// ...
}