473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How does one read in 32 bits at a time from a binary file?

I have a file "a.dat" containing 10^4 32 bit binary numbers. I need to
read in these numbers and deterimine if they are prime. The primality
part I will have to figure out later, but I was wondering if there is
anyone out there that can help me in reading in the binary numbers.
Basically, read in 32 bits and thats
the first number, read in another 32 bits and thats the 2nd
number, etc. Is is something like this?
fread(char, sizeofchar, numberofelements, filepointer)

Jul 22 '05 #1
7 5017
Well, 32 bits is 4 bytes, which is just an int (on i386, usually). So:

fstream file;
int n;
file_position = 0;
file.open("filename", ios::in | ios::binary);

file.seekg(file_position, ios::beg);
file.read(n, 4);

and then you just increase file_position by 4 each time you run those
last two lines. Hope that helps.

Jul 22 '05 #2
jd******@gmail.com wrote:
Well, 32 bits is 4 bytes, which is just an int (on i386, usually). So:
Would that not differ across compilers, even on i386?
I can imagine sizeof(int) == 2 and sizeof(long int) ==4.

I think the OP might want to either make sure that sizeof(int)
is indeed 4 or read in char[4] chunks, expecting a char to be
8 bits wide.
fstream file;
int n;
file_position = 0;
file.open("filename", ios::in | ios::binary);

file.seekg(file_position, ios::beg);
file.read(n, 4);

and then you just increase file_position by 4 each time you run those
last two lines. Hope that helps.


As a side note -- is it necessary to seek to the beginning of
a filestream after opening it? I personally never do that.

- J.
Jul 22 '05 #3
> I think the OP might want to either make sure that sizeof(int)
is indeed 4 or read in char[4] chunks, expecting a char to be
8 bits wide.
Absolutely. To do it properly, one really shouldn't hardcode it at
all, but just simply read sizeof(int) to an int. That way it will be
portable not only across compilers, but across different architectures
as well.
As a side note -- is it necessary to seek to the beginning of
a filestream after opening it? I personally never do that.


No, it definitely isn't, but if you're going to be reading in say, all
of a file, you'll want to do a seekg() each time you read (perhaps in a
for loop) to move your read head.

Jul 22 '05 #4
"jd******@gmail.com" wrote:
I think the OP might want to either make sure that sizeof(int)
is indeed 4 or read in char[4] chunks, expecting a char to be
8 bits wide.
Absolutely. To do it properly, one really shouldn't hardcode it at
all, but just simply read sizeof(int) to an int. That way it will be
portable not only across compilers, but across different architectures
as well.


???
I am not sure if I understand correctly what your wrote.
Portability with reading on a binary level is a hard thing.
That is: You write the file on one platform. Transport it to
another platform and read it there. Not only can sizeof(int)
differ on both platforms, but also can byte ordering (or
in case of floating point values the whole bit layout).
So binary portabitility is also easiliy achievable if those
2 programs which read and write the data, or compiled by the
same compiler, same compiler version on the same platform.
As a side note -- is it necessary to seek to the beginning of
a filestream after opening it? I personally never do that.


No, it definitely isn't, but if you're going to be reading in say, all
of a file, you'll want to do a seekg() each time you read (perhaps in a
for loop) to move your read head.


Why? Just read on. The 'read head' will move just by 'read'-ing
the file.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
I was just saying that you would want to use 'sizeof(int)' rather than
simply '4.' That's all.

As for the read head thing, I'm sorry, I completely mixed myself up.
You're right :-)

Jul 22 '05 #6
"jd******@gmail.com" wrote:

I was just saying that you would want to use 'sizeof(int)' rather than
simply '4.' That's all.
Then I read more into your post then you intended.
Apologies.
It sounded to me as: just use sizeof(int) instead of 4 and all
your portability problems are solved.

As for the read head thing, I'm sorry, I completely mixed myself up.
You're right :-)


No problem. It happens to all of us :-)
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7
Karl Heinz Buchegger wrote:
I am not sure if I understand correctly what your wrote.
Portability with reading on a binary level is a hard thing.
That is: You write the file on one platform. Transport it to
another platform and read it there.


That's right. I have more or less concluded that binary files are a dead
loss for average use. I inherited a program with a binary file format
once, and had to put in some additions. Result: entirely new struct
definitions, then converter programs, etc. Then I put it on Linux as
well as DOS. Big trouble, as padding was different on the same
processor. But after I converted to a text format, all these troubles
went away (provided I wrote once-only bullet-proof cr/lf recognising code).

I assigned a version of the file a version number. Then, when new
versions added fields to the data, I upped the number. Output always
sent out the latest version, but input code had this form:

read original fields
if (version > 1) { read some more fields } else set sensible defaults
if (version > 2) { read yet more... } else set defaults...

I am up to about version 20 and have an app that is almost
unrecogniseable from the original, but it still reads and correctly
loads the very first file format (not counting the binary ones, that is!).

I had to delete some fields in one version. Even that can be sensibly
handled in an obvious way.

--
Ron House ho***@usq.edu.au
http://www.sci.usq.edu.au/staff/house
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zulik | last post by:
Hello, I have a problem with MessageDigest MD5 in Java. I want to calculate the digest from a file, encode it in base64 and display it. Now, according to MD5 spec, digest shall be 128 bits so,...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
15
by: Claudio Grondi | last post by:
Let's consider a test source code given at the very end of this posting. The question is if Python allows somehow access to the bytes of the representation of a long integer or integer in...
5
by: Oyvind Eriksen | last post by:
Hello. I need to read bits from bytes in a file. I have code that works but it's very slow. Can anybody help me? The code I have is: private bool GetBit(byte b, int pos) { return ((b &...
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
18
by: Indian.croesus | last post by:
Hi, If I am right Endianness is CPU related. I do not know if the question is right in itself but if it is then how does C handle issues arising out of Endianness. I understand that if we pass...
3
by: phwashington | last post by:
I am new to C++ and have a data file I want to read, which was stored in binary. I have looked at the data with a hex editor and it appears to be correct. Whenever I try to read it though as an...
18
by: gregpinero | last post by:
It seems like this would be easy but I'm drawing a blank. What I want to do is be able to open any file in binary mode, and read in one byte (8 bits) at a time and then count the number of 1 bits...
1
by: tom thomas | last post by:
Dear sir, i am very new to c# and i request you to... please look into the problem The following is the code(please see the code below:) which i uses to open the binary file,currently it works...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.