unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
StdCtrls;
type
{ TForm1 }
PeopleData = Packed Record
Name : String[255];
LastName : String[255];
Age: String[3];
end;
FileData = Packed Record
People : Array of PeopleData;
LongString : String;
end;
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
SGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure generateDefValues();
Procedure loadfromfile();
private
public
end;
Const
ArchData = 'example.dat';
var
Form1: TForm1;
LongString : String = '';
ArrPeople : Array of PeopleData;
CustomFile : FileData;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
if not fileexists(ArchData) then generateDefValues()
else loadfromfile();
end;
procedure TForm1.Button1Click(Sender: TObject); // save to file
var
MemStr: TMemoryStream;
begin
MemStr := TMemoryStream.Create;
try
MemStr.Write(CustomFile, SizeOf(CustomFile));
MemStr.SaveToFile(ArchData);
finally
MemStr.Free;
end;
end;
Procedure TForm1.loadfromfile();
var
MemStr: TMemoryStream;
FileStr: TFileStream;
Begin
setlength(Customfile.People,0);
Customfile.LongString:='';
MemStr := TMemoryStream.Create;
try
FileStr := TFileStream.Create(ArchData, fmOpenRead);
MemStr.CopyFrom(FileStr,Sizeof(FileStr));
FileStr.Free;
MemStr.Read(CustomFile, SizeOf(CustomFile));
finally
MemStr.Free;
end;
memo1.lines.add(LongString);
// fill Sgrid with loaded Arrpeople
end;
Procedure TForm1.generateDefValues();
var
counter : integer;
long : String = '';
Begin
counter := 500+random(1000);
for counter := 1 to counter do
long := long+chr(random(26)+65);
LongString := long;
memo1.lines.add(long);
SetLength(ArrPeople,Sgrid1.RowCount-1);
for counter := 0 to length(ArrPeople)-1 do
begin
Arrpeople[counter].Name:=Sgrid1.Cells[0,counter+1];
Arrpeople[counter].LastName:=Sgrid1.Cells[1,counter+1];
Arrpeople[counter].Age:=Sgrid1.Cells[2,counter+1];
end;
CustomFile := Default(FileData);
CustomFile.People := Arrpeople;
CustomFile.LongString:=LongString;
end;
end.