fstream, overloading >> and << for serialization of binary classes

W

Wayne Marsh

Hello,

Is it considered sane/good practice to write a global operator for the
insertion and extraction operators of an fstream in binary mode to
serialize a binary class, or are they strictly meant for formatted text
input and output?

Let's imagine, for example, that I had a standard Windows BMP file (I am
aware that C++ has no concept of a BMP - this is simply putting my
question in a simple context). If I wanted to load it into a BMP class,
would it be horrible and wrong to write an operator to enable an
ifstream in binary mode to do this?

BMP myBitmap;
ifstream fileIn("filename.bmp", ios::binary);
fileIn >> myBitmap;

I'm guessing that it would, as the functionality would depend on the
flags of the stream, and that's hideous and specific.

However, there doesn't seem to be an obvious way of
serializing/de-serializing to specific binary formats in a nice way in
standard C++. Perhaps there is something in Boost...?

(I am aware that I could just do fileIn.read, but I am working with a
streaming format and would rather my access methods would fit in with a
pre-existing framework).

Thanks.

- Wayne
 
J

Jim Langston

Wayne Marsh said:
Hello,

Is it considered sane/good practice to write a global operator for the
insertion and extraction operators of an fstream in binary mode to
serialize a binary class, or are they strictly meant for formatted text
input and output?

Let's imagine, for example, that I had a standard Windows BMP file (I am
aware that C++ has no concept of a BMP - this is simply putting my
question in a simple context). If I wanted to load it into a BMP class,
would it be horrible and wrong to write an operator to enable an ifstream
in binary mode to do this?

BMP myBitmap;
ifstream fileIn("filename.bmp", ios::binary);
fileIn >> myBitmap;

I'm guessing that it would, as the functionality would depend on the flags
of the stream, and that's hideous and specific.

However, there doesn't seem to be an obvious way of
serializing/de-serializing to specific binary formats in a nice way in
standard C++. Perhaps there is something in Boost...?

(I am aware that I could just do fileIn.read, but I am working with a
streaming format and would rather my access methods would fit in with a
pre-existing framework).

Thanks.

- Wayne

Personally, I think it would be a useful thing to write a operator>> for a
BMP or other classes, and yes, it would depend on the flags of the ifstream,
but don't overrides already?
 
D

Denise Kleingeist

Hello Wayne!
Wayne said:
Is it considered sane/good practice to write a global operator for the
insertion and extraction operators of an fstream in binary mode to
serialize a binary class, or are they strictly meant for formatted text
input and output?

The latter! IOStreams are intended for formatted textual I/O only. The
"binary" flag has a relatively minor effect (and none on some
platforms).
If you want to use binary (formatted; it is still formatted in some
form
although not human readable but adhering to some conventions to
transfer binary data) formatted stream, create classes which internally
depend on the actual I/O layer of IOStreams, i.e. their stream buffers.
James Kanze and Dietmar Kuehl have discussed a system like this in
the past.

Good luck, Denise
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top