|
Boost : |
Subject: Re: [boost] Interest in simple unformatted binary stream I/O?
From: Beman Dawes (bdawes_at_[hidden])
Date: 2011-05-27 07:46:24
On Fri, Apr 29, 2011 at 3:52 AM, Mathias Gaunard
<mathias.gaunard_at_[hidden]> wrote:
> On 24/04/2011 14:12, Beman Dawes 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 |
>> 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
>
> I think it's a bad idea.
>
> If you want to do that kind of thing, you should use the streambuf
> abstraction directly, along with its sgetn and sputn member functions.
> It will be much more efficient.
Interesting. But what specifically do you think is a bad idea? The
whole idea of doing binary (aka raw) I/O to a stream or just the way I
have implemented it using read() and write()?
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk