binary file input

Discussion in 'C++' started by Nick R, Dec 28, 2003.

  1. Nick R

    Nick R Guest

    I need to know how to input bits from a *.wav file.

    I have tried this:
    #include <fstream>
    ....
    ifstream fin;
    fin.open("/root/foofile.wav", ios::in|ios::binary);

    and then inputting individual bits as a bool, and then joining them as an
    int (which represents one 8 bit byte) like this (I don't care about leading
    zeroes):tin

    int byte=0;
    bool bit;
    for(int i=0; i<8; i++)
    {
    bit=fin.get();
    if(bit)
    byte*=10;
    byte++;
    }tin

    but somehow, it's not working. Can anyone find some error in the way I'm
    inputting my bits, opening my file, etc?

    I am running Slackware Linux, and I use gcc(++) to compile stuff.
    Nick R, Dec 28, 2003
    #1
    1. Advertising

  2. "Nick R" <> wrote...
    > I need to know how to input bits from a *.wav file.
    >
    > I have tried this:
    > #include <fstream>
    > ...
    > ifstream fin;
    > fin.open("/root/foofile.wav", ios::in|ios::binary);
    >
    > and then inputting individual bits as a bool, [...]


    There is [usually] no way to read individual bits from
    a file. The minimal portion you must be able to read
    is a byte/char.

    Victor
    Victor Bazarov, Dec 29, 2003
    #2
    1. Advertising

  3. Nick R

    CWadd64208 Guest

    If I read the individual byte w/ fin.get() as an int, what kind of info might I
    get? 8 1s and 0s in binary, a decimal number, or what?

    That is, would it recognize the file as something like:
    212343464677 (dec)
    where a byte would be 3 digits, 000 to 255

    or seomthing like
    8c9f12fea2 (hex)
    where 2 digits represents a byte

    or something like:
    232352746516 (oct)
    where 4 digits represent one byte

    or something like:
    100011101101010100110, where the only way to read in info is 8 digits at a time
    (1 byte)

    or waht?
    CWadd64208, Dec 29, 2003
    #3
  4. "CWadd64208" <> wrote...
    > If I read the individual byte w/ fin.get() as an int, what kind of info

    might I
    > get? 8 1s and 0s in binary, a decimal number, or what?


    On the system you're using probably 8 bits (check CHAR_BIT value)

    >
    > That is, would it recognize the file as something like:
    > 212343464677 (dec)
    > where a byte would be 3 digits, 000 to 255
    >
    > or seomthing like
    > 8c9f12fea2 (hex)
    > where 2 digits represents a byte
    >
    > or something like:
    > 232352746516 (oct)
    > where 4 digits represent one byte
    >
    > or something like:
    > 100011101101010100110, where the only way to read in info is 8 digits at a

    time
    > (1 byte)
    >
    > or waht?
    Victor Bazarov, Dec 29, 2003
    #4
  5. Nick R <> wrote in message news:<JmJHb.30564$PK3.15499@okepread01>...
    > I need to know how to input bits from a *.wav file.
    >
    > I have tried this:
    > #include <fstream>
    > ...
    > ifstream fin;
    > fin.open("/root/foofile.wav", ios::in|ios::binary);
    >
    > and then inputting individual bits as a bool, and then joining them as an
    > int (which represents one 8 bit byte) like this (I don't care about leading
    > zeroes):tin
    >
    > int byte=0;
    > bool bit;
    > for(int i=0; i<8; i++)
    > {
    > bit=fin.get();
    > if(bit)
    > byte*=10;
    > byte++;
    > }tin
    >
    > but somehow, it's not working. Can anyone find some error in the way I'm
    > inputting my bits, opening my file, etc?
    >
    > I am running Slackware Linux, and I use gcc(++) to compile stuff.


    Well, you could always read a string and then process it all...
    Chris Mantoulidis, Dec 29, 2003
    #5
  6. Nick R

    Rolf Magnus Guest

    CWadd64208 wrote:

    > If I read the individual byte w/ fin.get() as an int, what kind of
    > info might I get? 8 1s and 0s in binary, a decimal number, or what?


    You get a char value with the data. What you get out of that depends on
    how you interpret it.

    > That is, would it recognize the file as something like:
    > 212343464677 (dec)
    > where a byte would be 3 digits, 000 to 255
    >
    > or seomthing like
    > 8c9f12fea2 (hex)
    > where 2 digits represents a byte
    >
    > or something like:
    > 232352746516 (oct)
    > where 4 digits represent one byte
    >
    > or something like:
    > 100011101101010100110, where the only way to read in info is 8 digits
    > at a time (1 byte)
    >
    > or waht?


    All the above are just different ways to print the data.
    Rolf Magnus, Dec 29, 2003
    #6
  7. Nick R

    Jeff Schwab Guest

    Nick R wrote:
    > I need to know how to input bits from a *.wav file.
    >
    > I have tried this:
    > #include <fstream>
    > ...
    > ifstream fin;
    > fin.open("/root/foofile.wav", ios::in|ios::binary);
    >
    > and then inputting individual bits as a bool, and then joining them as an
    > int (which represents one 8 bit byte) like this (I don't care about leading
    > zeroes):tin
    >
    > int byte=0;
    > bool bit;
    > for(int i=0; i<8; i++)
    > {
    > bit=fin.get();
    > if(bit)
    > byte*=10;
    > byte++;
    > }tin


    Is "tin" a typo?

    > but somehow, it's not working. Can anyone find some error in the way I'm
    > inputting my bits, opening my file, etc?
    >
    > I am running Slackware Linux, and I use gcc(++) to compile stuff.


    Why are you representing a byte with an int? Because bytes are such
    fundamental units, they have their own type, misleadingly named "char".
    The name is because characters generally are represented by integers,
    through a mapping called a "character set." Similarly, "fin.get( )"
    will *not* return a boolean 1 or 0, as you might expect; instead, it
    will return the integer that represents the corresponding character in
    your local character set. You can represent such values intuitively
    with single quotes, as in '1' and '0'.

    I notice you are multiplying by ten. That's how base-ten numerals are
    left-shifted. You aren't reading base ten, are you? You're reading
    base two. Because this happens to be the base on which most computers
    are built, and because left-shifting is such a common operation, this
    task has its very own operator in C++. It looks just like the insertion
    operator "<<" you may have seen used with output streams.

    Also, what purpose does "bit" serve? Is it just to hold a value between
    one line and the next? Lots of people do that when they think it makes
    the code clearer, but I don't think it does here. However, may I
    suggest you at least move the definition of "bit" down to the point of
    initialization? A similar recommendation applies to the opening of the
    fstream; why didn't you initialize it properly, rather than calling
    "fin.open" manually on the next line? Either way, you don't have to
    specify ios::in; it is implied by the fact that you are constructing an
    "ifstream", not just an "fstream".

    By the way, are you running this program as root? You probably don't
    want to do that. However, if you try running this program as a normal
    user, it may not be able to read the file from root's home directory.

    Here's what I think you want. This is a very naive implementation,
    since it does not check for erros in the input stream. For example, if
    the file cannot be read, or if the input contains a '2' instead of a '1'
    or a '0', this program will not notice.

    #include <iostream>

    struct Byte
    {
    typedef unsigned char Value;

    Value value;

    operator unsigned char const ( ) const { return value; }
    };

    std::eek:stream&
    operator << ( std::eek:stream& out, Byte const& b )
    {
    Byte::Value value = b.value;

    int i = std::numeric_limits< Byte::Value >::digits;

    while( --i >= 0 )
    {
    out << ( ( b.value & ( 1 << i ) ) ? '1' : '0' );
    }

    return out;
    }

    std::istream&
    operator >> ( std::istream& in, Byte& b )
    {
    Byte::Value value = ( in.get( ) == '1' );

    int i = std::numeric_limits< Byte::Value >::digits;

    while( --i )
    {
    value = ( value << 1 ) | ( in.get( ) == '1' );
    }

    b.value = value;

    return in;
    }


    #include <fstream>

    int main( int argc, char** argv )
    {
    Byte byte;

    while( *++argv )
    {
    std::ifstream fin( *argv, std::ios::binary );

    fin >> byte;
    std::cout << byte << '\n';
    }
    }
    Jeff Schwab, Dec 29, 2003
    #7
    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. Clemens Park

    binary file input with cin

    Clemens Park, Sep 21, 2003, in forum: C++
    Replies:
    5
    Views:
    737
    David B. Held
    Sep 21, 2003
  2. news.hku.hk
    Replies:
    2
    Views:
    491
    Nick Hounsome
    Jan 2, 2004
  3. Gianni Mariani
    Replies:
    0
    Views:
    361
    Gianni Mariani
    Jan 6, 2004
  4. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    893
    James Kanze
    Apr 28, 2008
  5. Jim
    Replies:
    6
    Views:
    683
Loading...

Share This Page