ifstream binary read into unsigned char b[LENGTH]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven T. Hatton

    #1

    ifstream binary read into unsigned char b[LENGTH]

    What is the best way to read data from a file into a fixed size array of
    unsigned char? This is a file holding only a SHA1 digest. What I would
    really like to do is initialize the ifstream with the buffer allocated to
    hold the data, seekg(ios_base: :end) and magically have the data appear in
    the buffer. I believe something close to this can be done.

    I might allocate the array:

    const unsigned LENGTH = 256;
    unsigned char b[LENGTH];

    Then cast it to a std::streambuf

    std::streambuf sb(b[LENGTH]);

    But can I construct std::ifstream that uses that same memory to hold the
    data from its file? How to I prevent reading past the end of my buffer, or
    the end of the file data?
    --
    NOUN:1. Money or property bequeathed to another by will. 2. Something handed
    down from an ancestor or a predecessor or from the past: a legacy of
    religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
    from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
  • Rolf Magnus

    #2
    Re: ifstream binary read into unsigned char b[LENGTH]

    Steven T. Hatton wrote:
    [color=blue]
    > What is the best way to read data from a file into a fixed size array of
    > unsigned char? This is a file holding only a SHA1 digest. What I would
    > really like to do is initialize the ifstream with the buffer allocated to
    > hold the data, seekg(ios_base: :end) and magically have the data appear in
    > the buffer.[/color]

    Why should a seek read any data?
    [color=blue]
    > I believe something close to this can be done.
    >
    > I might allocate the array:
    >
    > const unsigned LENGTH = 256;
    > unsigned char b[LENGTH];
    >
    > Then cast it to a std::streambuf
    >
    > std::streambuf sb(b[LENGTH]);[/color]

    I don't think that you can be sure that the buffer will contain exactly the
    data you want. After all, it's a buffer that's meant for internal use.
    [color=blue]
    > But can I construct std::ifstream that uses that same memory to hold the
    > data from its file?[/color]

    The purpose of streams is text formatting. If you want to read binary data,
    just use a filebuf directly.
    [color=blue]
    > How to I prevent reading past the end of my buffer, or the end of the file
    > data?[/color]

    Your idea seems overly complicated to me. Just use the filebuf::sgetn( )
    function to read as many bytes as you want and you're done.

    Comment

    • Steven T. Hatton

      #3
      Re: ifstream binary read into unsigned char b[LENGTH]

      Rolf Magnus wrote:
      [color=blue]
      > Steven T. Hatton wrote:
      >[color=green]
      >> What is the best way to read data from a file into a fixed size array of
      >> unsigned char? This is a file holding only a SHA1 digest. What I would
      >> really like to do is initialize the ifstream with the buffer allocated to
      >> hold the data, seekg(ios_base: :end) and magically have the data appear in
      >> the buffer.[/color]
      >
      > Why should a seek read any data?[/color]

      As I understand things, that will (usually?) force the file to be completely
      read into memory.
      [color=blue]
      > Your idea seems overly complicated to me. Just use the filebuf::sgetn( )
      > function to read as many bytes as you want and you're done.[/color]

      Will that copy the bytes from one location in memory to another? That's
      what I want to avoid. This is basically the same thing as move semantics.
      Rather than copying the contents of the buffer, I simply want to take
      ownership of it.
      --
      NOUN:1. Money or property bequeathed to another by will. 2. Something handed
      down from an ancestor or a predecessor or from the past: a legacy of
      religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
      from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/

      Comment

      Working...