Hi,

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:

std::ofstream file_sink("encrypted.bin", std::ios_base::binary | std::ios_base::out);
    basic_xor_filter<char> filter(*(file_sink.rdbuf()), 0x7F);
    std::ostream output_stream(&filter);
    output_stream << "Hello World" << std::endl;
    output_stream.flush();

The 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:

streambuf* s = &filter;
    cout.rdbuf( s );
    cout << "Hello World\n";

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?

Thanks very much.

Member Avatar for jencas
streambuf* s = &filter;
        streambuf* old = cout.rdbuf(s);
        cout << "Hello World" << endl;
        cout.flush();
        cout.rdbuf(old);

How does your 'filter' look? Maybe more problems here!?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.