Boost logo

Boost :

From: Edwin Vane (revane_at_[hidden])
Date: 2007-05-02 17:02:02


Hi,

I'd like to create an iostream that writes data in binary format to a
pre-allocated block of memory. I'm already using array_sink to handle
the 'write to a pre-allocated block of memory' part:

  char* my_data = new char[1024];

  io::stream_buffer<io::array_sink> buf(my_data,1024);
  std::ostream os(&buf);

  int i = 15;
  float f = 5.1345;
  os << i;
  os << f;
  os << "Hello World!";
  os << 'c';

However, all the data written to my_data is in text format (i.e. 15 is
encoded in 2 bytes 0x31 0x35 instead of 4 (assuming 32-bit integers)).
What I'd like is something like file_sink where you specify the mode as
binary. The 'mode' seems to be a Device-level decision (as it is with
basic_file_sink) so I'm guessing array_sink is not what I need afterall.
Does something exist for writing binary data to a block of memory?

-- 
Edwin Vane
RapidMind, Inc.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk