X
xieyj
Unregistered / Unconfirmed
GUEST, unregistred user!
我自己写了一个,请问一下为什么这样,只能写入一条记录.代码如下,怎么遍历读取出数据库里的所有记录:
如果不够,我还有一个同样问题300分相送
Type
TFileHead = Record
Version : string[10];
UpdateDate: TDatetime;
end;
Type
TFileData = Record
Name : string[30];
Phone : string[30];
Age : integer;
end;
Var
TFile: File;
FileHead: TFileHead;
FileData: TFileData;
FileSize: integer;
Const FileName: string = 'C:/a.xmd';
Function WriteRecord(Var F: File;
Data: TFileData): Boolean;
Function ReadRecord(Var F: File;
Data: TFileData): Boolean;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
If Not FileExists(FileName) then
begin
AssignFile(TFile, FileName);
ReWrite(TFile, 1);
FileHead.Version := '1.0.0';
FileHead.UpdateDate := Now;
BlockWrite(TFile, FileHead, SizeOf(FileHead));
CloseFile(TFile);
end;
AssignFile(TFile, FileName);
Reset(TFile, 1);
BlockRead(TFile, FileHead, SizeOf(FileHead));
BlockRead(TFile, FileData, SizeOf(FileData), FileSize);
edit1.Clear;
edit2.Clear;
edit3.Clear;
While FileSize = SizeOf(TFileData)do
begin
BlockRead(TFile, FileData, SizeOf(FileData), FileSize);
end;
CloseFile(TFile);
end;
{为何这里只能写入一条记录}
Function WriteRecord(Var F: File;
Data: TFileData): Boolean;
begin
Try
AssignFile(F, FileName);
ReWrite(F);
Try
BlockWrite(F, FileData, SizeOf(FileData) + 1);
Result := True;
Except
Result := False;
end;
Finally
CloseFile(F);
end;
end;
Function ReadRecord(Var F: File;
Data: TFileData): Boolean;
begin
Try
AssignFile(F, FileName);
Reset(F);
Try
BlockRead(F, FileData, SizeOf(FileData));
Result := True;
Except
Result := False;
end;
Finally
CloseFile(F);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FileData.Name := edit1.Text;
FileData.Phone := edit2.Text;
FileData.Age := StrToInt(edit3.Text);
WriteRecord(TFile, FileData);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ReadRecord(TFile, FileData);
edit1.Text := FileData.Name;
edit2.Text := FileData.Phone;
edit3.Text := IntToStr(FileData.Age);
end;
如果不够,我还有一个同样问题300分相送
Type
TFileHead = Record
Version : string[10];
UpdateDate: TDatetime;
end;
Type
TFileData = Record
Name : string[30];
Phone : string[30];
Age : integer;
end;
Var
TFile: File;
FileHead: TFileHead;
FileData: TFileData;
FileSize: integer;
Const FileName: string = 'C:/a.xmd';
Function WriteRecord(Var F: File;
Data: TFileData): Boolean;
Function ReadRecord(Var F: File;
Data: TFileData): Boolean;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
If Not FileExists(FileName) then
begin
AssignFile(TFile, FileName);
ReWrite(TFile, 1);
FileHead.Version := '1.0.0';
FileHead.UpdateDate := Now;
BlockWrite(TFile, FileHead, SizeOf(FileHead));
CloseFile(TFile);
end;
AssignFile(TFile, FileName);
Reset(TFile, 1);
BlockRead(TFile, FileHead, SizeOf(FileHead));
BlockRead(TFile, FileData, SizeOf(FileData), FileSize);
edit1.Clear;
edit2.Clear;
edit3.Clear;
While FileSize = SizeOf(TFileData)do
begin
BlockRead(TFile, FileData, SizeOf(FileData), FileSize);
end;
CloseFile(TFile);
end;
{为何这里只能写入一条记录}
Function WriteRecord(Var F: File;
Data: TFileData): Boolean;
begin
Try
AssignFile(F, FileName);
ReWrite(F);
Try
BlockWrite(F, FileData, SizeOf(FileData) + 1);
Result := True;
Except
Result := False;
end;
Finally
CloseFile(F);
end;
end;
Function ReadRecord(Var F: File;
Data: TFileData): Boolean;
begin
Try
AssignFile(F, FileName);
Reset(F);
Try
BlockRead(F, FileData, SizeOf(FileData));
Result := True;
Except
Result := False;
end;
Finally
CloseFile(F);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
FileData.Name := edit1.Text;
FileData.Phone := edit2.Text;
FileData.Age := StrToInt(edit3.Text);
WriteRecord(TFile, FileData);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ReadRecord(TFile, FileData);
edit1.Text := FileData.Name;
edit2.Text := FileData.Phone;
edit3.Text := IntToStr(FileData.Age);
end;