Re: Writing Binary Files with TFileStream
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Wed, 23 Nov 2005 00:30:50 -0600
Robert Kilroy wrote:
Here is the function to READ: (Which works just fine) var fid : Word; l : Byte; fieldid, value : String; FS : TFileStream; try FS := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone); tData.Clear; 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);
When you read the string length, you read one byte.
Here is 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 := pntData.Names[i];
brwValue := pntData.ValueFromIndex[i];
x := StrToInt(brwFieldID);
FS.Write(x, SizeOf(x));
l := length(brwValue);
FS.Write(l, SizeOf(l));
FS.Write(brwValue[1], l);
When you write the string length, you write four bytes.
You were also more careful about empty strings in your reading function than you are in your writing function.
-- Rob .
- References:
- Writing Binary Files with TFileStream
- From: Robert Kilroy
- Writing Binary Files with TFileStream
- Prev by Date: Re: Loaded memory image differs from map file
- Next by Date: Re: Writing Binary Files with TFileStream
- Previous by thread: Re: Writing Binary Files with TFileStream
- Next by thread: copy one character from a string.
- Index(es):
Relevant Pages
|
|