Byte I/O

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    Byte I/O

    I'm trying to read a binary file, but the read() function in fstream takes
    char*. What I really want are unsigned chars. How can I do this?

    Thanks,

    --Brian



  • Rolf Magnus

    #2
    Re: Byte I/O

    Brian wrote:
    [color=blue]
    > I'm trying to read a binary file, but the read() function in fstream
    > takes char*. What I really want are unsigned chars. How can I do
    > this?[/color]

    fstream is just a typedef for basic_fstream<c har>, i.e. a char based
    stream. Read() takes a pointer to the stream's template argument type.
    So if you create your file as basic_fstream<u nsigned char>, read() will
    take an unsigned char*.

    Comment

    • Micah Cowan

      #3
      Re: Byte I/O

      Rolf Magnus <ramagnus@t-online.de> writes:
      [color=blue]
      > Brian wrote:
      >[color=green]
      > > I'm trying to read a binary file, but the read() function in fstream
      > > takes char*. What I really want are unsigned chars. How can I do
      > > this?[/color]
      >
      > fstream is just a typedef for basic_fstream<c har>, i.e. a char based
      > stream. Read() takes a pointer to the stream's template argument type.
      > So if you create your file as basic_fstream<u nsigned char>, read() will
      > take an unsigned char*.[/color]

      AIUI, you would then also have to define a specialization of
      char_traits<> for unsigned char, as that is not defined by the Standard.

      Another common practice seems to be to use
      reinterpret_cas t<char *>() on a pointer into a buffer of unsigned
      char.

      --
      Micah J. Cowan
      micah@cowan.nam e

      Comment

      Working...