Trying to override streambuf to use with cout
I'm trying to grab all the input sent to cout to modify and redirect it. I used the example from http://www.dreamincode.net/code/snippet2499.htm, which works fine using the following code:
The basic_xor_filter class overrides streambuf and simply XORs the input characters before sending them to the provided streambuf (in this case, a file). My problem occurs when I try setting cout's rdbuf() to filter:
std::ofstream file_sink("encrypted.bin", std::ios_base::binary | std::ios_base::out);
basic_xor_filter<char> filter(*(file_sink.rdbuf()), 0x7F);
output_stream << "Hello World" << std::endl;
This code causes a glibc "double free or corruption (!prev)" error. Could anyone tell me where I am going wrong? Perhaps I just don't understand the concepts properly?
streambuf* s = &filter;
cout.rdbuf( s );
cout << "Hello World\n";
Thanks very much.