this slowpoke moves

Create FileStream

type
  TNewStream = record
    Name: string[50];
    vorname: string[50];
  end;

  TComputer = record
    Name: string[30];
    cpu: string[30];
end;

var
  Form1: TForm1;
  NewStream: TNewStream;
  Computer: TComputer;
  Stream: TFileStream;
  
//

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    Stream := TFileStream.Create('c:\test.dat', fmOpenReadWrite);
  except
    Stream := TFileStream.Create('c:\test.dat', fmCreate);
  end;

  //save 2 records for TNewStream and TComputer
  NewStream.Name    := 'Name';
  NewStream.vorname := 'NewName';
  Stream.WriteBuffer(NewStream, SizeOf(NewStream));

  NewStream.Name    := 'Name';
  NewStream.vorname := 'FirstName';
  Stream.WriteBuffer(NewStream, SizeOf(TNewStream));

  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;

//load records from TPerson to listbox1 and
//load records from TComputer to listbox2
procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
begin
  try
    //open read only
    Stream := TFileStream.Create('c:\test.dat', fmOpenRead);
  except
    ShowMessage('Datei konnte nicht geladen werden.');
    Exit;
  end;

  //set variable i to the record count
  //Read records TPerson
  for i := 2 downto 1 do
  begin
    Stream.ReadBuffer(NewStream, SizeOf(TNewStream));
    Listbox1.Items.Add(NewStream.vorname + ' ' + NewStream.Name);
  end;

  //Read Records TComputer
  for i := 2 downto 1 do
  begin
    Stream.ReadBuffer(Computer, SizeOf(TComputer));
    Listbox1.Items.Add(Computer.Name + ' ' + Computer.cpu);
  end;

  Stream.Free;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate