Re: Help reading structured binary files



Ya, as I'm getting deeper into these individual files, I'm finding that
many are corrupted. I'd say that 40% of the time I don't get my 255-255
terminator. Obviously I need to get whatever I can out of them, so I
had to junk the ReadBuffer. WAY too many exceptions to catch. It seemed
to slow the whole process down. Here is the refined function. (Thanks
Rob, much speed improvements)

function TForm1.LoadWRK(filename : String) : TStringlist;
var
tmpList : TStringlist;
fid : Word;
l : Byte;
fieldid, value, mydata : String;
FS : TFileStream;
begin
tmpList := TStringList.Create;
FS := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone);
try
while (FS.Position < FS.Size) do
begin
FS.Read(fid, SizeOf(fid));
if fid = $ffff then break;
fieldid := IntToStr(fid);
FS.Read(l, 1);
SetLength(value, l);
if l > 0 then
FS.Read(value[1], l);
mydata := mydata+fieldid+'='+value+#13;
end;
finally
FS.Free;
end;
tmpList.Text := mydata;
result := tmpList;
end;

The above gets me to where I need to be.

One thing that got it moving along was removing the stringlist from the
loop and assigning it at the end. That in itself boosted things along
nicely. It seems that there isn't a real tight way of doing this, no
matter what I still have to return the data in some key/value pair set.
A C++ guy I know suggested memory mapped files, seems like more
trouble than its worth. At this point we're only talking about
fractions of a second.

.