type
TPerson = record
Name: string[50];
vorname: string[50];
end;
TComputer = record
Name: string[30];
cpu: string[30];
end;
var
Form1: TForm1;
Person: TPerson;
Computer: TComputer;
Stream: TFileStream;
implementation
{$R *.DFM}
//Speichern resp. Erstellen von Datei
//Save or create the file
procedure TForm1.Button1Click(Sender: TObject);
begin
try
Stream := TFileStream.Create('c:\test.dat', fmOpenReadWrite);
except
Stream := TFileStream.Create('c:\test.dat', fmCreate);
end;
//2 Einträge pro Record
//save 2 records for TPerson and TComputer
Person.Name := 'Grossenbacher';
Person.vorname := 'Simon';
Stream.WriteBuffer(Person, SizeOf(TPerson));
Person.Name := 'Stutz';
Person.vorname := 'Thomas';
Stream.WriteBuffer(Person, SizeOf(TPerson));
Computer.Name := 'Delphi';
Computer.cpu := 'Intel';
Stream.WriteBuffer(Computer, SizeOf(TComputer));
Computer.Name := 'Win';
Computer.cpu := 'AMD';
Stream.WriteBuffer(Computer, SizeOf(TComputer));
Stream.Free;
end;
//lädt alle daten von TPerson in listbox1 und
//daten von TComputer in Listbox2
//load records from TPerson to listbox1 and
//load records from TComputer to listbox2
procedure TForm1.Button2Click(Sender: TObject);
var
i: Integer;
begin
try
// nur lesen öffnen
//open read only
Stream := TFileStream.Create('c:\test.dat', fmOpenRead);
except
ShowMessage('Datei konnte nicht geladen werden.');
Exit;
end;
//variable i auf anzahl Einträge setzen
//set variable i to the record count
//Einlesen von TPerson
//Read records TPerson
for i := 2 downto 1 do
begin
Stream.ReadBuffer(Person, SizeOf(TPerson));
Listbox1.Items.Add(Person.vorname + ' ' + Person.Name);
end;
//Einlesen von TComputer
//Read Records TComputer
for i := 2 downto 1 do
begin
Stream.ReadBuffer(Computer, SizeOf(TComputer));
Listbox2.Items.Add(Computer.Name + ' ' + Computer.cpu);
end;
Stream.Free;
end;
|