Writing Binary Files with TFileStream
- From: "Robert Kilroy" <kilroy@xxxxxxxxx>
- Date: 22 Nov 2005 11:16:05 -0800
Greetings,
After searching the web and Usenet for a few days, I haven't been able
to resolve an issue with a write function I have. (See below)
The data being written comes out of a TStringlist (tData) and I'm using
the tData.Names[x] to hold a field ID (Numeric) and the value
associated with it in the Strings. My read function works perfectly, so
I tried to reverse the process to write it. But all I get back of
mumbo-jumbo.
I was wondering if someone could point me in the right direction.
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);
tData.Values[trim(fieldid)] := trim(value);
end;
FS.Free;
result := True;
except
result := False;
end;
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);
end;
e := $FFFF;
FS.Write(e, SizeOf(e));
FS.Free;
result := True;
except
result := False;
end;
My usual tasks are DB oriented, so binary files are somewhat new. Note
that when ending the file, I need to have 2 bytes: 255 255. I've tried
using WriteBuffer as well, same result. I guess it's a variable-type
issue of some sort, but the solution escapes me at this point. I'd
really appreciate some help from anyone willing to provide it.
- RK
.
- Follow-Ups:
- Re: Writing Binary Files with TFileStream
- From: Rob Kennedy
- Re: Writing Binary Files with TFileStream
- From: Robert Kilroy
- Re: Writing Binary Files with TFileStream
- Prev by Date: Re: TMozillaBrowser
- Next by Date: Re: Writing Binary Files with TFileStream
- Previous by thread: "HTTP 1.1 302 found" message with Indy
- Next by thread: Re: Writing Binary Files with TFileStream
- Index(es):
Relevant Pages
|
|