1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
type
TStructRec = packed record
ID: Integer;
Nom: String[50];
Memo: String[255];
Date: TDateTime;
end;
procedure TFrmTestFichier.BtnLoadClick(Sender: TObject);
var
Fichier: File of TStructRec;
TmpStructRec: TStructRec;
i: Integer;
begin
if not FileExists(EdPathStructFile.Text) then begin
ListBoxRecords.Items.Clear();
ListBoxRecords.Items.Add('New');
Exit; // Rien à Charger
end else begin
ListBoxRecords.Items.Clear();
ListBoxRecords.Items.Add('New');
AssignFile(Fichier, EdPathStructFile.Text);
Reset(Fichier); //
while not Eof(Fichier) do begin
Read(Fichier, TmpStructRec);
i := ListBoxRecords.Items.Add(IntToStr(TmpStructRec.ID) + ' : ' + TmpStructRec.Nom);
ListBoxRecords.Items.Objects[i] := Pointer(TmpStructRec.ID);
end;
end;
end;
procedure TFrmTestFichier.BtnEcrireClick(Sender: TObject);
var
Fichier: File of TStructRec;
TmpStructRec: TStructRec;
begin
ZeroMemory(@TmpStructRec, SizeOf(TmpStructRec));
TmpStructRec.ID := StrToInt(EdID.Text);
TmpStructRec.Nom := EdName.Text;
TmpStructRec.Memo := EdMemo.Text;
TmpStructRec.Date := Now();
AssignFile(Fichier, EdPathStructFile.Text);
if not FileExists(EdPathStructFile.Text) then begin
ReWrite(Fichier);
end else begin
Reset(Fichier);
Seek(Fichier, TmpStructRec.ID); // Magique le BlockSize est SizeOf(TStructRec);
end;
Write(Fichier, TmpStructRec);
CloseFile(Fichier);
BtnLoad.Click();
end; |
Partager