Re: fread/fwrite Bits

Discussion in 'C Programming' started by CBFalconer, Jan 3, 2004.

  1. CBFalconer

    CBFalconer Guest

    Tom Timmermann wrote:
    > I am trying to read a binary file which has bit level codes.
    > example with spaces added for clarity:
    > 00 00000001 00000001 10 11 01 00001111 10
    > 00 indicates read the next 2 bytes as short
    > 10 and 11 read no further
    > 01 read the next byte as unsigned char
    > Any ideas on how to do this ?
    > If I read as unsigned char and use bit shift to extract the 2 bit
    > code, how do I control the file pointer position at the bit level ?

    This is better suited for c.l.c, to which I have cross-posted.

    Your first task is to decide on the actual file format. C bytes
    are not necessarily octets, so you must first decide if each byte
    can contain more than 8 bits of information or not. Then you have
    to write code to convert the incoming byte stream to a bit stream,
    which involves making decisions as to which end of the byte holds
    the earliest bit.

    You don't control the incoming file stream at the bit level. You
    have to provide the appropriate buffering and manipulation.

    If you limit your solutions to a world where bytes are octets, and
    the earliest bit is the most significant bit in a byte, that
    decision should be isolated in the stream conversion code. After
    that all will be simple. All you really need then is a
    getnextbit() routine.

    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <> USE worldnet address!
    CBFalconer, Jan 3, 2004
    1. Advertisements

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

    problem using fread, fwrite, and fsetpos

    Brady, Jul 17, 2003, in forum: C Programming
    Dave Thompson
    Jul 21, 2003
  2. Guest

    fread / fwrite error

    Guest, Aug 1, 2003, in forum: C Programming
    Emmanuel Delahaye
    Aug 7, 2003
  3. mazsx
    Toni Uusitalo
    Nov 11, 2005
  4. fread fwrite struct

    , Mar 30, 2006, in forum: C Programming
    Michael Mair
    Mar 31, 2006
  5. Richard Hsu

    fwrite() fails when called after fread()

    Richard Hsu, Apr 11, 2006, in forum: C Programming
    Richard Hsu
    Apr 11, 2006

Share This Page