post Trying to override streambuf to use with cout

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 1
Reputation: rob_l_f is an unknown quantity at this point 
Solved Threads: 0
rob_l_f rob_l_f is offline Offline
Newbie Poster

post Trying to override streambuf to use with cout

 
0
  #1
Jan 11th, 2009
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:

  1. std::ofstream file_sink("encrypted.bin", std::ios_base::binary | std::ios_base::out);
  2. basic_xor_filter<char> filter(*(file_sink.rdbuf()), 0x7F);
  3. std::ostream output_stream(&filter);
  4. output_stream << "Hello World" << std::endl;
  5. 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:

  1. streambuf* s = &filter;
  2. cout.rdbuf( s );
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: post Trying to override streambuf to use with cout

 
0
  #2
Jan 12th, 2009
  1. streambuf* s = &filter;
  2. streambuf* old = cout.rdbuf(s);
  3. cout << "Hello World" << endl;
  4. cout.flush();
  5. cout.rdbuf(old);

How does your 'filter' look? Maybe more problems here!?
Last edited by jencas; Jan 12th, 2009 at 10:36 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC