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

Discussion in 'C++' started by Wayne Marsh, Dec 23, 2006.

  1. Wayne Marsh

    Wayne Marsh Guest

    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
     
    Wayne Marsh, Dec 23, 2006
    #1
    1. Advertising

  2. Wayne Marsh

    Jim Langston Guest

    "Wayne Marsh" <> wrote in message
    news:458cc7ae$0$32029$...
    > 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?
     
    Jim Langston, Dec 23, 2006
    #2
    1. Advertising

  3. Re: fstream, overloading >> and << for serialization of binary classes

    Hello Wayne!
    Wayne Marsh wrote:
    > 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
     
    Denise Kleingeist, Dec 23, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    2,315
    deadsea
    Jan 2, 2005
  2. Armando
    Replies:
    6
    Views:
    779
    Armando
    Jan 29, 2004
  3. Replies:
    3
    Views:
    1,088
  4. Dimitri Ognibene
    Replies:
    4
    Views:
    825
    Dimitri Ognibene
    Sep 2, 2006
  5. Ramunas Urbonas
    Replies:
    1
    Views:
    427
    Dino Chiesa [Microsoft]
    Jul 27, 2004
Loading...

Share This Page