reading from a binary file

J

jesuraj

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?
 
M

mlimber

jesuraj said:
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
 
G

Gernot Frisch

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();
 
M

mlimber

Gernot said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top