Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storage of numbers in a binary file

Status
Not open for further replies.

chpicker

Programmer
Apr 10, 2001
1,316
Does anyone know how to read numbers stored in a binary file? For the most part I can translate them properly, but I can't figure out floating point numbers. For example, there is a number stored as type DOUBLE with a length of 4. I'm assuming this means it takes up 4 bytes on disk, but I cannot figure out how to take those 4 bytes and get the floating point number out of it. Can anyone help?
 
Never done this before but what does the value in the binary file look like? and how do you know it is a double?

Matt
 
I have a file specification for the file type that gives the layout of the records. Bytes 4-7 of each record are of type DOUBLE, and based on the data it should end up being a number with around 9 significant figures, including 6 decimal places. However, I have no idea how to decode that number out of the 4 bytes.
 
Do you by chance know a value and it's representation in the binary file? I am assuming that the binary file is in hex? I will play around with a hex file and see if i can figure out how microsoft writes doubles to a binary file.

Matt
 
ok... i copied my bootsec.dos file to temp.bin and here is what i did. I opened it as binary, wrote it to a file and tacked on 3 doubles at the end

double a = 3.14;
double b = 6.7987;
double c = 3456.890;

i tacked em on with out<<a<<b<<c;

here is what the file got appended to it

33 2E 31 34 36 2E 37 39 38 37 33 34 35 36 2E 38
39

this was viewed through a hex editor. The pattern I see is the double is represented by the second value. 2E represents the decimal point. Each 2 character word above is a value from 0 to 255 so it is 1 byte. What I would do is read in the first three bytes and do nothing (or whatever you need to with them) then

char my_double[4]; //read in bytes 4-7

for(int i=0;i<4;i++)
my_double=in.get();

now you can go through and find what you need

30 = 0
31 = 1
32 = 2
etc
2E = .

as long as you know it is just bytes 4-7 this should get you what you need. Also, these values are just the ascii equivalent in hex. Hopefully this will help you out.

Matt
 
Hmm...it looks like all it did was output it in ascii format. The numbers that the 4-byte DOUBLEs represent are lattitude and longitude coordinates, which could range from -180 to +180 and contain around 6-8 decimal places. So, for example, the number -94.972634 is somehow represented by a 4 byte value. Unfortunately, I don't know what any of the numbers in the file are supposed to be, or I could attempt a comparison.
 
You can write
#include<fstream>
using namespace std;
struct xfloat
{
union
{
float x;
char y[4];
}
operator float(){return x;}
operator=(float c){x=c;}
};
int main()
{
xfloat z=3.14;
ofstream x(&quot;binfloat.bin&quot;);
for(int i=0;i<4;i++)binfloat.put(z.y);
x.close();
ifsrteam xx(&quot;binfloat.bin&quot;);
for(int i=0;i<4;i++)z.y=binfloat.get();
.....
John Fill
1c.bmp


ivfmd@mail.md
 
well... i have worked on systems that have used 16 bit values as follows

bit 15 was 180
bit 14 was 90
bit 13 was 45
bit 12 was 27.5
etc...

and if i remember correctly this could represent just about any value. By seeing which bits are set you could determine the value. Maybe this is the way to look at it. I am shooting in the dark here though but just a thought.

Matt
 
Sorry, some mistakes:
ofstream x(&quot;binfloat.bin&quot;);
for(int i=0;i<4;i++)x.put(z.y);
x.close();
ifsrteam xx(&quot;binfloat.bin&quot;);
for(int i=0;i<4;i++)z.y=xx.get();
..... John Fill
1c.bmp


ivfmd@mail.md
 
Open your file as binary.
declare a double YourVariable //location for read data.
FILE* Inputfile; // input stream.

Open the file for binary read,
then use

fread(&YourVariable,sizeof(double),1,Inputfile).

this reads the number of bytes required to read a double value from a file and stores the read data at the location of Your Variable.

Then use YourVariable as normal.

Hope this helps sma.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top