|
Boost : |
Subject: Re: [boost] Interest in simple unformatted binary stream I/O?
From: Jamie Allsop (ja11sop_at_[hidden])
Date: 2011-04-25 08:40:49
On 24/04/11 13:12, Beman Dawes wrote:
> This proposal provides a very simple solution that works with standard
> library input and output streams. The one caveat is that the stream
> must be opened with filemode std::ios_base::binary to avoid certain
> data values being treated as line endings.
>
> int main()
> {
> fstream f("binary_stream_example.dat",
> std::ios_base::trunc | std::ios_base::in | std::ios_base::out |
> std::ios_base::binary);
>
> int32_t x = 0x01020304;
> int32_t y = 0;
>
> f<< bin(x); // write 4 bytes
> f.seekg(0);
> f>> bin(y); // read 4 bytes
>
> BOOST_ASSERT(x == y);
>
> return 0;
> }
I don't like bin() as others note. I think you have a couple of options.
- use binary() - to match use of std::ios_base::binary
- use raw() - which seems to imply exactly what you want
- use bytes() - just like your comments
- use as_binary() or as_raw() or as_bytes()
> For docs, header, and example code, see
> http://mysite.verizon.net/beman/binary_stream/binary_stream.html
> http://mysite.verizon.net/beman/binary_stream/binary_stream.hpp
> http://mysite.verizon.net/beman/binary_stream/binary_stream_example.cpp
>
> Is there interest in this for Boost?
Yes
> It seems way too small and simple to be a whole library itself. Are
> there any ideas where it should live and what namespace it should be
> in?
iostreams, utility, binary_stream? I'm thinking that some of what this
offers is complimentary to the endian library in that endian allows
streaming of raw values while preserving endianness.
Jamie
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk