Board index » delphi » Question re: Retrieving and manipulating data from binary file

Question re: Retrieving and manipulating data from binary file


2007-01-20 01:24:32 AM
delphi50
I need to iterate through a binary file and come up w/ output. I am using an export tool that exports the data into XML (which I can then use), but I want to read the original file so I can skip having to export the XML and then read that file into my program.
I have the structure of the file, and can read the appropriate data, but for two values, I can not for the life of me figure out how to retrieve the data correctly.
The structure is something like (I believe it is C):
unsigned int locx;
unsigned int locy;
double address;
char char1;
time_t filedate;
char zeros[19];
I've translated this into a structure in Delphi as follows:
sLocX : single;
sLocY : single;
dAddress : double;
cChar : array [0..1] of byte;
dFileDate : TDateTime;
cChar2 : array [0..19] of byte;
I can iterate through the file and populate sLocX, sLocY, dAddress, dFileDate (cChar and cChar2 aren't used). dAddress and dFileDate are used as is, but I need to do some further calculations to sLocX and sLocY.
How can I separate into distinct variables the sLocX and sLocY values so that the first two bytes are stored in sLocX1, the second two in sLocX2, the third in sLocX3 and the last two in sLocX4? Is this possible or do I need to change the type when retrieving data? (I know I need to read sLocX and sLocY as 4 bytes from the file b/c whenever I change this value, the resulting dAddress and FileDate end up being incorrect.)
Whenever I try and read directly to the sLocX1..4 by calling
binStream.Read(sLocX1, 1)
..
binStream.Read(sLocX4, 1)
I get a bad result. (I will know whether the result is bad by feeding the values into an equation and seeing what the answer is -- I have the desired outcome for each record from what's in the XML file.)
I've been working on the this for the past week or so and it is driving me mad. Any suggestions on how to accomplish this would be greatly appreciated.
 
 

Re:Question re: Retrieving and manipulating data from binary file

In article <45b0fed0$XXXX@XXXXX.COM>, "JRB" <XXXX@XXXXX.COM>writes:
Quote
The structure is something like (I believe it is C):
Yes it is C.

unsigned int locx;
unsigned int locy;
double address;
char char1;
time_t filedate;
char zeros[19];

I've translated this into a structure in Delphi as follows:

sLocX : single;
sLocY : single;
Bad translation: an unsigned int is definitely not a single!
Cardinal might do, but if the values never exceed 2 billion integer would do
just as well and if the values do exceed 2 billion DWord is the only option.
Quote
dAddress : double;
cChar : array [0..1] of byte;
why has this array appeared? ..Its just a char.
dFileDate : TDateTime;
cChar2 : array [0..19] of byte;
nearly but not quite:
char zeros[19] means an array of 19 chars. If the numbering starts at zero
the 19th one is zeros[18]
so array [0..18] of byte;
Quote

I can iterate through the file and populate sLocX, sLocY, dAddress,
dFileDate (cChar and cChar2 aren't used). dAddress and dFileDate are used
as is, but I need to do some further calculations to sLocX and sLocY.

How can I separate into distinct variables the sLocX and sLocY values so
that the first two bytes are stored in sLocX1, the second two in sLocX2,
the third in sLocX3 and the last two in sLocX4? Is this possible or do I
need to change the type when retrieving data?
Its probably best to retrieve the data according to the file specification
and then manipulate it to your own needs.
var slocx1,slocx2: integer;
slocx1:=slocx and $FFFF;
slocx2:=slocx shr 8;
- is one possible way of doing the manipulation you ask for.