Pliki Typowane

0

Mam mały problem. Przeczytalem artykuł Pliki Typowane i chcialem go troche zmodyfikowac... Oto kod oryginału...

unit MainFrm;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls;

type
TMainForm = class(TForm)
grbInfo: TGroupBox;
lblName: TLabel;
edtFName: TEdit;
lblNumber: TLabel;
edtNumber: TEdit;
lblDate: TLabel;
Date: TDateTimePicker;
btnSave: TButton;
cmbRec: TComboBox;
procedure cmbRecChange(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure ReadData(NumberOfRec : Byte);
procedure WriteDate;
procedure RefreshRec;
end;

TVideoRec = packed record
NazwaFilmu : String[30];
Numerek : WORD;
Data : TDateTime;
end;

TVideoFile = file of TVideoRec;

var
MainForm: TMainForm;

implementation

const
FileName = 'Data.dat';

{$R *.DFM}

procedure TMainForm.WriteDate;
var
VFile : TVideoFile;
VRec : TVideoRec;
FSize : Integer;
begin
AssignFile(VFile, FileName);
if not FileExists(FileName) then
Rewrite(VFile) else
Reset(VFile);

try
FSize := FileSize(VFile);
if FSize > 0 then
Seek(VFile, FSize);

with VRec do
begin
  NazwaFilmu := edtFname.Text;
  Numerek := StrToInt(edtNumber.Text);
  Data := Date.DateTime;
end;
Write(VFile, VRec);

finally
CloseFile(VFile);
RefreshRec;
end;
end;

procedure TMainForm.ReadData(NumberOfRec: Byte);
var
VFile : TVideoFile;
VRec : TVideoRec;
begin

AssignFile(VFile, FileName);
Reset(VFile);

try
Seek(VFile, NumberOfRec);
Read(VFile, VRec); do
begin
edtFName.Text := NazwaFilmu;
edtNumber.Text := IntToStr(Numerek);
Date.DateTime := Data;
end;
finally
CloseFile(VFile);
end;
end;

procedure TMainForm.cmbRecChange(Sender: TObject);
begin

ReadData(cmbRec.ItemIndex);
end;

procedure TMainForm.btnSaveClick(Sender: TObject);
begin

WriteDate;
end;

procedure TMainForm.RefreshRec;
var
VFile : TVideoFile;
I : Integer;

begin

AssignFile(VFile, FileName);
if not FileExists(FileName) then
Exit else

Reset(VFile);
cmbRec.Items.Clear;

begin
For I := 0 to FileSize(VFile) -1 do
cmbRec.Items.Add('Rekord nr: ' + IntToStr(i));
end;

CloseFile(VFile);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
RefreshRec;
end;

end.

A to zmodyfikowana wersja jednej procedury:

procedure TMainForm.RefreshRec;
var
VFile : TVideoFile;
I : Integer;
Vrec: TVideoRec;
begin

AssignFile(VFile, FileName);
if not FileExists(FileName) then
Exit else

Reset(VFile);
cmbRec.Items.Clear;
Read(VFile, VRec);

With Vrec do
begin
For I := 0 to FileSize(VFile) -1 do
cmbRec.Items.Add(NazwaFilmu);
end;

CloseFile(VFile);
end;

CHcialem zeby w Comboboxie zamiast Rekord nr ... wyswietlana byla nazwa filmu... Pomocy. Z gory dzieki.

//Uzywaj znaczników i do opisywania kodu

0

Moze inaczej... chcialbym zrobic program - encyklopedie za pomoca plikow typowanych. Potrafie zapisac dane do pliku ale mam problem ze zrobieniemwyszukiwania i wyswietlenia konkretnego rekordu z haslem i definicja.

Plz pomozcie!

0

Najprościej to oprzeć to na pliku INI.. (tak na początek..)
http://4programmers.net/view.php?id=1

0

Odczytuj po kolei rekordy, a potem dodawaj do comboboxa co ci trzeba :)

A jeżeli nie wiesz jak korzystać z plików, to:

!!!!!! SPAM !!!!!!
http://vogel.iglu.cz
!!!!!! SPAM !!!!!!

0

A nie prościej i rozsądniej byłoby to zrobić jako bazę danych. Hmm? Jesli sie nie znasz to rozumiem cie ale bazy danych to przydatna rzecz. B-|

0

Właśnie w tm problem ze nie bardzo umiem sie tym poslugiwac :/

1 użytkowników online, w tym zalogowanych: 0, gości: 1