reading from a binary file

Discussion in 'C++' started by jesuraj, Jan 18, 2006.

  1. jesuraj

    jesuraj Guest

    Hi,

    how can i read input from a data file which contains binary or hex
    values.
    I have to use the exact binary data for further processing.only
    limited number of bits are taken form the file(64 bits) for current
    processing.Once the processing is done the next 64 bits must be
    applied.Can this be done?
    jesuraj, Jan 18, 2006
    #1
    1. Advertising

  2. jesuraj

    mlimber Guest

    jesuraj wrote:
    > how can i read input from a data file which contains binary or hex
    > values.


    I think I know what you mean, but let's just be more precise: you don't
    mean that the file is a text file that contains strings of characters
    representing binary or hex digits, right? For example, your file would
    *not* look like this:

    0x12345678 0xC0FFEE 0xDEADBEEF

    or like this:

    1110111010101010011101b 1111110000000b

    IOW, when you say "binary," you mean the data is not in text mode,
    right? If so, you shouldn't refer to it as binary or hex "values",
    since the base of the number only affects the representation, not the
    value. Rather, refer to it as "binary data" or "data in binary format".

    > I have to use the exact binary data for further processing.only
    > limited number of bits are taken form the file(64 bits) for current
    > processing.Once the processing is done the next 64 bits must be
    > applied.Can this be done?


    Assuming that (1) you mean that you need to open the file in binary
    mode, (2) by "must be applied" you mean "must be read and processed",
    and (3) 64 is evenly divisible by the number of bits in a char, you can
    do it like this:

    #include <fstream>
    #include <climits>
    #include <cassert>
    using namespace std;

    int main()
    {
    // Is 64 evenly divisible by the no. of bits in a char?
    assert( 0 == 64 % CHAR_BIT );
    std::ifstream file( "file.bin", std::ios_base::binary );
    while( file )
    {
    char buffer[ 64 / CHAR_BIT ];
    if( file.read( buffer, sizeof( buffer ) ) )
    {
    // Do processing of 64 bits here ...
    }
    }
    }

    You could also read larger chunks (or the whole file) into memory at
    once and then process that buffer, but you didn't ask about that. You
    may also want to use something other than a char buffer, especially if
    your platform has a 64-bit type, and you will probably want more
    sophisticated error handling. But that gives you an idea.

    Cheers! --M
    mlimber, Jan 18, 2006
    #2
    1. Advertising

  3. jesuraj wrote:
    > how can i read input from a data file which contains binary or hex
    > values.


    Using 'read' or 'get'

    > [...]


    V
    Victor Bazarov, Jan 18, 2006
    #3

  4. > I have to use the exact binary data for further processing.only
    > limited number of bits are taken form the file(64 bits) for current
    > processing.Once the processing is done the next 64 bits must be
    > applied.Can this be done?


    No.



    .... just joking...

    std::istream is(filename, std::ios::modeOpen|std::ios::typeBinary);
    while(is.good())
    {
    char i64[64];
    is.read(&i64, 64);
    }
    is.close();
    Gernot Frisch, Jan 18, 2006
    #4
  5. jesuraj

    mlimber Guest

    Gernot Frisch wrote:
    > > I have to use the exact binary data for further processing.only
    > > limited number of bits are taken form the file(64 bits) for current
    > > processing.Once the processing is done the next 64 bits must be
    > > applied.Can this be done?

    >
    > No.
    >
    >
    >
    > ... just joking...
    >
    > std::istream is(filename, std::ios::modeOpen|std::ios::typeBinary);


    You don't need the modeOpen (which I presume is obscure shorthand for
    "in" or "out"), since ifstreams are always "in".

    > while(is.good())


    More canonical would be:

    while( is )

    > {
    > char i64[64];


    The OP said 64-bit, not 64-byte, which is presumably your intent here.

    > is.read(&i64, 64);
    > }
    > is.close();


    Informational: closing of the file is automatic thanks to the
    destructor and should be omitted unless there's a good reason to close
    it early (which there may or may not be in the OP's code).

    Cheers! --M
    mlimber, Jan 18, 2006
    #5
    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. Neo
    Replies:
    0
    Views:
    469
  2. El Durango

    reading binary file.

    El Durango, Dec 8, 2004, in forum: Java
    Replies:
    1
    Views:
    7,555
    Gordon Beaton
    Dec 8, 2004
  3. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    924
    James Kanze
    Apr 28, 2008
  4. Guest
    Replies:
    6
    Views:
    1,665
    Guest
    Apr 25, 2010
  5. Jim
    Replies:
    6
    Views:
    728
Loading...

Share This Page