Help reading structured binary files
- From: "Robert Kilroy" <kilroy@xxxxxxxxx>
- Date: 17 Jul 2005 21:50:50 -0700
Greetings,
I was given a task to read some binary files that we will be dumping
into our database. The files are in key/value pairs in the format of:
Field ID = 2 bytes ( (Byte2 * 256) + Byte1 )
Field Length = 1 byte (0-255)
Field Data = 1 to 255 bytes
Files terminated by 2 bytes of 255
I'm not entirely what these files are, I was told "Put these in the
database".
The bytes are converted into CHRs and I'm returning them in a
Stringlist for easier manipulation. The function below works fine, but
I'm frustrated at its speed and was wondering if someone could give me
a shove in a different direction. This is really the first time I've
messed with binary files like this and I'm sure I'm doing it all wrong.
:)
function TForm1.LoadWRK(filename : String) : TStringlist;
var
i : Integer;
tmpList : TStringlist;
fid1, fid2, l, c : Byte;
fieldid, value : String;
MS : TMemoryStream;
begin
tmpList := TStringList.Create;
MS := TMemoryStream.Create;
try
MS.LoadFromFile(filename);
MS.Position := 0;
while (MS.Position < MS.Size) do
begin
MS.Read(fid1, 1);
MS.Read(fid2, 1);
if (fid1 = 255) AND (fid2 = 255) then
break;
fieldid := IntToStr((fid2*256)+fid1);
MS.Read(l, 1);
value := '';
for i := 1 to l do
begin
MS.Read(c, 1);
value := value + chr(c);
end;
tmpList.Values[trim(fieldid)] := trim(value);
end;
finally
MS.Free;
end;
result := tmpList;
end;
I was wondering if I could use some sort of Record structure, but
either way it seems I need to read the third byte to get the length of
the data. Maybe a BlockRead? But is there a faster way to convert each
byte into its CHR value for a string?
I'd appreciate any help provided. We have anywhere from 300-500 files
at a time to process and any speed improvements would really help.
Thanks much in advance!
- RK
.
- Follow-Ups:
- Re: Help reading structured binary files
- From: Bruce Roberts
- Re: Help reading structured binary files
- From: Rob Kennedy
- Re: Help reading structured binary files
- Prev by Date: Re: which memory address space?
- Next by Date: Re: Help reading structured binary files
- Previous by thread: which memory address space?
- Next by thread: Re: Help reading structured binary files
- Index(es):
Relevant Pages
|