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
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