binary I/O

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vw at iep dot tu-graz dot ac dot at

    binary I/O

    Hello

    Is there something more elegant in C++ to read/write data
    binary into a stream than for example:

    out.write(reint erpret_cast<con st char *>(&long_var), sizeof(long_var ));
    in.read(reinter pret_cast<char *>(&unsigned_va r), sizeof(unsigned _var));

    The need for reinterpret_cas t<> looks suspicious.

    Thanks
    Volker

  • WW

    #2
    Re: binary I/O

    vw at iep dot tu-graz dot ac dot at" <"vw at iep dot tu-graz dot ac dot
    at wrote:[color=blue]
    > Hello
    >
    > Is there something more elegant in C++ to read/write data
    > binary into a stream than for example:
    >
    > out.write(reint erpret_cast<con st char *>(&long_var),
    > sizeof(long_var )); in.read(reinter pret_cast<char *>(&unsigned_va r),
    > sizeof(unsigned _var));
    >
    > The need for reinterpret_cas t<> looks suspicious.[/color]

    Yes. Use static_cast. *Never* use reinterpret_cas t, unless you are doing
    something deliberately non-portable.

    --
    WW aka Attila


    Comment

    • Kevin Goodsell

      #3
      Re: binary I/O

      WW wrote:
      [color=blue]
      > vw at iep dot tu-graz dot ac dot at" <"vw at iep dot tu-graz dot ac dot
      > at wrote:
      >[color=green]
      >>Hello
      >>
      >>Is there something more elegant in C++ to read/write data
      >>binary into a stream than for example:
      >>
      >>out.write(rei nterpret_cast<c onst char *>(&long_var),
      >>sizeof(long_v ar)); in.read(reinter pret_cast<char *>(&unsigned_va r),
      >>sizeof(unsign ed_var));
      >>
      >>The need for reinterpret_cas t<> looks suspicious.[/color]
      >
      >
      > Yes. Use static_cast. *Never* use reinterpret_cas t, unless you are doing
      > something deliberately non-portable.
      >[/color]

      You can't static_cast from long* to const char* directly, though. You'd
      have to use double static_casts, right?

      static_cast<con st char *>(static_cast< void *>(&long_var))

      Or I suppose you could define a new type of cast:

      template<class D, class S>
      D safe_reinterpre t(const S &source)
      {
      return static_cast<D>( static_cast<voi d *>(source));
      }

      OK, that's rather ugly, and not very safe at all, but it might be a good
      solution with some more work.

      -Kevin
      --
      My email address is valid, but changes periodically.
      To contact me please use the address from a recent posting.

      Comment

      • Gianni Mariani

        #4
        Re: binary I/O

        Kevin Goodsell wrote:
        ....[color=blue]
        >
        > Or I suppose you could define a new type of cast:
        >
        > template<class D, class S>
        > D safe_reinterpre t(const S &source)[/color]

        D terribly_unsafe _reinterpret(co nst S &source)

        bad ... bad bad :=)
        [color=blue]
        > {
        > return static_cast<D>( static_cast<voi d *>(source));
        > }
        >
        > OK, that's rather ugly, and not very safe at all, but it might be a good
        > solution with some more work.[/color]

        What kind of work ?

        Portability in this case is certainly an issue, it has all the problems
        associated with writing binary files - see the post I wrote 7 days ago.




        G

        Comment

        • Kevin Goodsell

          #5
          Re: binary I/O

          Gianni Mariani wrote:
          [color=blue]
          > Kevin Goodsell wrote:
          > ...
          >[color=green]
          >>
          >> Or I suppose you could define a new type of cast:
          >>
          >> template<class D, class S>
          >> D safe_reinterpre t(const S &source)[/color]
          >
          >
          > D terribly_unsafe _reinterpret(co nst S &source)
          >
          > bad ... bad bad :=)
          >[color=green]
          >> {
          >> return static_cast<D>( static_cast<voi d *>(source));
          >> }
          >>
          >> OK, that's rather ugly, and not very safe at all, but it might be a
          >> good solution with some more work.[/color]
          >
          >
          > What kind of work ?[/color]

          Well... if you could make sure the destination type is void*, char*,
          unsigned char*, or signed char* (or a const and/or volatile qualified
          variant of one of those), and the source type is a pointer, then it
          should be safe, I think. Don't know how you'd do that, though.

          But even as it is I don't think it's less safe than a reinterpret_cas t
          (other than the fact that it misrepresents itself as being safe).

          -Kevin
          --
          My email address is valid, but changes periodically.
          To contact me please use the address from a recent posting.

          Comment

          • Gianni Mariani

            #6
            Re: binary I/O

            Kevin Goodsell wrote:[color=blue]
            > Gianni Mariani wrote:
            >[color=green]
            >> Kevin Goodsell wrote:
            >> ...
            >>[color=darkred]
            >>>
            >>> Or I suppose you could define a new type of cast:
            >>>
            >>> template<class D, class S>
            >>> D safe_reinterpre t(const S &source)[/color]
            >>
            >>
            >>
            >> D terribly_unsafe _reinterpret(co nst S &source)
            >>
            >> bad ... bad bad :=)
            >>[color=darkred]
            >>> {
            >>> return static_cast<D>( static_cast<voi d *>(source));
            >>> }
            >>>
            >>> OK, that's rather ugly, and not very safe at all, but it might be a
            >>> good solution with some more work.[/color]
            >>
            >>
            >>
            >> What kind of work ?[/color]
            >
            >
            > Well... if you could make sure the destination type is void*, char*,
            > unsigned char*, or signed char* (or a const and/or volatile qualified
            > variant of one of those), and the source type is a pointer, then it
            > should be safe, I think. Don't know how you'd do that, though.[/color]

            OK - that's a good idea. I should probably do that for the network
            order template.

            Comment

            Working...