Re: Writing Binary Files with TFileStream



"Robert Kilroy" <kilroy@xxxxxxxxx> schreef in bericht
news:1132687317.937714.202220@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Correction on the WRITE function:
>
> var
> FS : TFileStream;
> i : Integer;
> l : LongInt;
> x, e : Word;
> brwFieldID, brwValue : String;
> begin
> try
> if FileExists(fname) then
> FS := TFileStream.Create(fname, fmOpenWrite)
> else
> FS := TFileStream.Create(fname, fmCreate);
> for i := 0 to tData.Count - 1 do
> begin
> brwFieldID := tData.Names[i];
> brwValue := tData.ValueFromIndex[i];
> x := StrToInt(brwFieldID);
> FS.Write(x, SizeOf(x));
> l := length(brwValue);
> FS.Write(l, SizeOf(l));
> FS.Write(brwValue[1], l);
> end;
> e := $FFFF;
> FS.Write(e, SizeOf(e));
> FS.Free;
> result := True;
> except
> result := False;
> end;

It looks like the mirror of the read function, except:
- I can't see the difference between the letter l and the number 1 (my
advice: never use the letter l). There may be a 1 where there should be an
l.
- the result of IntToStr may not be the same as StrToInt when called with
large _word_ arguments rather than integer arguments.
- the read has a check on zero-length strings, the write does not.

Tom

-


.