Memphis
23-01-2009, 04:37 PM
hi there, i've been having problems with writing some variant sized records. i have no idea what is going wrong, anyone can confirm my code would be really appreciated.
the type: (there is a reason why it is an array of byte, because i tried many other ways to get it to work and still no luck)
TRule = record
data : array of byte;
end;
PRule = ^TRule;
the writing of the data, first i created a test record.
New(nr);
SetLength(nr.data, 8 + 6);
Integer((@nr.data[0])^) := 1;
Integer((@nr.data[4])^) := 1;
PAnsiChar((@nr.data[8])^) := 'Admin'+#0;
rules.Add(nr); // (cant be bothered to make as a linked list until i get it working)
now writing it
var
datalen: DWord;
i: Integer;
nr: PRule;
f: file of byte;
begin
AssignFile(f, './rules.rul');
ReWrite(f);
for i := rules.Count-1 downto 0 do begin
nr := rules[i];
datalen := Length(nr.data);
BlockWrite(f, datalen, 4);
BlockWrite(f, nr.data, datalen);
end;
CloseFile(f);
and finally reading it
AssignFile(f, './rules.rul');
Reset(f);
while not eof(f) do begin
BlockRead(f, datalen, 4);
GetMem(nr, datalen);
BlockRead(f, nr, datalen);
rules.Add(nr);
end;
CloseFile(f);
thanks in advance for any help.
;MM;
the type: (there is a reason why it is an array of byte, because i tried many other ways to get it to work and still no luck)
TRule = record
data : array of byte;
end;
PRule = ^TRule;
the writing of the data, first i created a test record.
New(nr);
SetLength(nr.data, 8 + 6);
Integer((@nr.data[0])^) := 1;
Integer((@nr.data[4])^) := 1;
PAnsiChar((@nr.data[8])^) := 'Admin'+#0;
rules.Add(nr); // (cant be bothered to make as a linked list until i get it working)
now writing it
var
datalen: DWord;
i: Integer;
nr: PRule;
f: file of byte;
begin
AssignFile(f, './rules.rul');
ReWrite(f);
for i := rules.Count-1 downto 0 do begin
nr := rules[i];
datalen := Length(nr.data);
BlockWrite(f, datalen, 4);
BlockWrite(f, nr.data, datalen);
end;
CloseFile(f);
and finally reading it
AssignFile(f, './rules.rul');
Reset(f);
while not eof(f) do begin
BlockRead(f, datalen, 4);
GetMem(nr, datalen);
BlockRead(f, nr, datalen);
rules.Add(nr);
end;
CloseFile(f);
thanks in advance for any help.
;MM;