472,985 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

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
4 2402
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.