Writing Binary Files with TFileStream



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

.



Relevant Pages

  • Re: Families in Germany
    ... Henning Böttcher (born in Hildesheim/Lower Saxony near Hannover) ... I am searching for the following Surnames in the southwestern area ... WALTEMATH, var. ...
    (soc.genealogy.german)
  • Re: Families in Germany
    ... I am searching the following surnames in the Hannover area of Germany: ... WALTEMATH, var. ...
    (soc.genealogy.german)
  • Re: search in dfm files
    ... > i need a tool for searching in dfm files that can restrict to a certain classname. ... > inherited MyPanel: TPanel ... procedure TForm1.FormDestroy(Sender: TObject); ...
    (borland.public.delphi.thirdpartytools.general)
  • Re: prototype autocompleter 2 variables
    ... i'm sorry for my poor english. ... var answer = undefined; ... USENET is not a real-time communications medium. ...
    (comp.lang.javascript)
  • Re: Refresh
    ... sorry for the inconvenience..I'm new to usenet. ... functionality works fine until the user presses the Reload button then ... reload issue in FireFox. ... var d = w.document; ...
    (comp.lang.javascript)