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