I have a Class procedure named LoadData which read records to dynamic array(1 based) of records written to a file. Initially array size is set to 2 and each time a record is read , array size is incremented using the SetLength procedure.
When I am running this code, after reading the records, following error is returned .
'Segmentation fault (core dumped)'
I am wondering why is this happening. Please help ..
type
TPerson = record
name : String[20];
age : Integer;
gender : String[10];
phNum : String[15];
end;
type
TPersonDetails = class
names : array of Tperson;
count : Integer;
procedure SaveData;
procedure LoadData;
procedure Display;
end;
procedure TPersonDetails.SaveData;
var
f : file of TPerson;
i : Integer;
begin
{$i+}
AssignFile(f,'/home/user1/pers.bat');
ReWrite(f);
{$i+}
try
for i := 1 to count - 1 do begin
BlockWrite(f,names[i],SizeOf(names[i]));
end;
finally
WriteLn('File Write Successfull');
CloseFile(f);
end;
end;
procedure TPersonDetails.LoadData;
var
i : Integer;
f : file of TPerson;
begin
i := 2;
SetLength(names,i);
count := 1;
{$i-}
AssignFile(f,'/home/user1/pers.bat');
Reset(f);
{$i+}
try
while not EOF(f) do begin
BlockRead(f,names[count],SizeOf(names[count]));
SetLength(names,i+1);
count := count + 1;
end;
finally
CloseFile(f);
WriteLn('Read successfully');
end;
end;
After calling TPersonDetails.LoadData
Read successfully
Segmentation fault (core dumped)