Subject: Re: [boost] Interest in simple unformatted binary stream I/O?
From: Fernando Pelliccioni (fpelliccioni_at_[hidden])
Date: 2011-10-04 16:01:02
On Sun, Apr 24, 2011 at 9:12 AM, Beman Dawes <bdawes_at_[hidden]> wrote:
> There have been many requests on Boost and various C++ newsgroups for
> unformatted binary I/O. For example, in 2003 Neal Becker wrote:
> I wonder if anyone has code for implementing unformatted I/O?
> What I have in mind is
> for the simple case where the application that reads data knows
> the data types, so
> this is not as complicated as the general marshalling situation.
> 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 |
> int32_t x = 0x01020304;
> int32_t y = 0;
> f << bin(x); // write 4 bytes
> f >> bin(y); // read 4 bytes
> BOOST_ASSERT(x == y);
> return 0;
> For docs, header, and example code, see
> Is there interest in this for Boost?
> 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
I made a small modification to your example and it stopped working.
Do not compile. ADL issue. Because binary_data and const_binary_data are
defined within detail namespace.
std::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 << boost::bin(x);
f >> boost::bin(y);
BOOST_ASSERT(x == y);
error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'boost::detail::binary_data<T>' (or there is no acceptable
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk