|
Boost : |
Subject: Re: [boost] Interest in simple unformatted binary stream I/O?
From: Beman Dawes (bdawes_at_[hidden])
Date: 2011-04-24 21:23:21
On Sun, Apr 24, 2011 at 3:18 PM, Vicente BOTET <vicente.botet_at_[hidden]> wrote:
>
>
> Hi,
>
>...
>> 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 guess that we can not mix binary and non binary formats. How a binary stream woks for non binary codes?
It works fine, as long as the formats are such that there is never a
question as to what you are dealing with. You can't do line oriented
operations on portions that have binary values that might be mistaken
for newline characters.
> I don·t know if a manipulator that changes the mode globaly can be defined so the following will work
>
> f << bin << x1 << x2; // write 4+4 bytes
> f.seekg(0);
> f >> bin >> y1 >> y2; // read 4+4 bytes
I share Steven's concern that bin in this usage could be misleading. I
also don't see how to do that without modifying the standard library.
bin() isn't meant to replace read() or write(); they still have their
place, and can be used to implement read() or write() templates that
can handle ranges or structs, among other things. I've done that
several times, but don't have a Boost quality implementation ready
just yet.
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk