将自定义的记录集合(记录条数不定,格式一致,都是些标准类型)存入文件中,怎么操作?(100分)

  • 主题发起人 主题发起人 yancey
  • 开始时间 开始时间
Y

yancey

Unregistered / Unconfirmed
GUEST, unregistred user!
不用数据库,仅仅写入文件,但最好写成两进制文件,并能方便的读取还原
 
Type
MyRecord = Record
Name: String[8];
Age: integer;
end;
RecordFile = File of MyRecord;

Var
RecordFile1: RecordFile;
RecordFile2: File of MyRecord;
 
我做过这个
打开一个filestream,一条条往里write就是了
读取的时候也是一条条读出来
 
unit Unit1;

interface

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

type
RecordItem=record
RecordID: integer;
RecordName: string[20];
end;

TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
RecordArray:array [1..10] of RecordItem;
F:File of RecordItem;
begin
AssignFile(F, 'record.fil');
Rewrite(F);
for i:=1 to 10 do
begin
with RecordArray do
begin
RecordID:=i;
RecordName:='aaa';
end;
write(F, RecordArray);
end;
CloseFile(F);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
RecordBuff:RecordItem;
F:File of RecordItem;
begin
AssignFile(F, 'record.fil');
Reset(F);
while not eof(F) do
begin
read(F, RecordBuff);
Listbox1.Items.Add(inttostr(RecordBuff.RecordID));
Listbox1.Items.Add(RecordBuff.RecordName);
end;
CloseFile(F);
end;

end.
 
多人接受答案了。
 
后退
顶部