467,877 Members | 1,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,877 developers. It's quick & easy.

reading from a binary file

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?

Jan 18 '06 #1
  • viewed: 2146
Share:
4 Replies
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

Jan 18 '06 #2
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();
Jan 18 '06 #3
jesuraj wrote:
how can i read input from a data file which contains binary or hex
values.
Using 'read' or 'get'
[...]


V
Jan 18 '06 #4
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

Jan 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by john smith | last post: by
6 posts views Thread by KevinD | last post: by
50 posts views Thread by Michael Mair | last post: by
7 posts views Thread by John Dann | last post: by
30 posts views Thread by siliconwafer | last post: by
6 posts views Thread by arne.muller | last post: by
9 posts views Thread by Use*n*x | last post: by
3 posts views Thread by The Cool Giraffe | last post: by
6 posts views Thread by efrenba | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.