文件保存问题(50分)

  • 主题发起人 主题发起人 风云再起
  • 开始时间 开始时间

风云再起

Unregistered / Unconfirmed
GUEST, unregistred user!
我声明了一个结构,
然后把他保存为文件
eg:type
tUser=record
name:string;
sex:string;
end;
var
fuser:Tuser;
fUserfile:file of tuser;
begin
fuser.name:='aa';
fuser.name:='man';
assign(.....);
......
如果现在我想删除fuser.name:='aa'; and fuser.sex:='man'
怎么办
有个人给了我段代码,大家看看,有点问题,不知在哪儿
unit Main;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
lbUser: TListBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
UserList:TList;//链表
RecFile:TFileStream;//文件
procedure LoadUsers(ARecFile:TFileStream);
procedure SaveUsers(ARecFile:TFileStream);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
type
PStudent=^TStudent;
TStudent=record
Name:String[20];
Sex:String[10];
end;

{ TForm1 }

procedure TForm1.LoadUsers(ARecFile: TFileStream);
var
PStu:PStudent;
i:Integer;
begin
with ARecFile do
begin
Position:=0;
while Position<Size do
begin
New(PStu);
Read(PStu^,sizeof(TStudent));
UserList.Add(PStu);
end;
for i:=0 to UserList.Count-1 do
begin
PStu:=UserList.Items;
Dispose(PStu);
end;
end;
end;

procedure TForm1.SaveUsers(ARecFile: TFileStream);
var
i:Integer;
begin
with ARecFile do
begin
Size:=0;
for i:=0 to UserList.Count-1 do
Write(TStudent(UserList.Items^),sizeof(TStudent));
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
UserList:=TList.Create;
if FileExists('C:/1.dat') then
RecFile:=TFileStream.Create('C:/1.dat',fmOpenReadWrite)
else
RecFile:=TFileStream.Create('C:/1.dat',fmCreate);
LoadUsers(RecFile);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
SaveUsers(RecFile);
UserList.Free;
RecFile.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
PStu:PStudent;
begin
for i:=0 to 9 do
begin
New(PStu);
PStu^.Name:='Name'+IntToStr(i);
PStu^.Sex:='Sex'+IntToStr(i);
UserList.Add(PStu);
end;
for i:=0 to 9 do
begin
PStu:=UserList.Items;
Dispose(PStu);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i:Integer;
begin
//显示文件数据
with lbUser.Items do
begin
BeginUpdate;
Clear;
for i:=0 to UserList.Count - 1 do
Add('姓名:'+TStudent(UserList^).Name
+#9'性别:'+TStudent(UserList^).Sex);
EndUpdate;
end;
end;

end.
 
上面代碼問題不大, 我就幫你增加刪除一個記錄的代碼吧:
procedure TForm1.DeleteAUser(AUser:TUser);
var
I:Integer;
CurUser:PUser; //應該為TUser的指針類型,假定為PUser
FindIndex:Integer;
begin
FindIndex:=-1;
for I:=0 to UserList.Count-1 do
begin
CurUser:=UserList.Items;
if (CurUser.Name=AUser.Name) and (CurUser.Sex=AUser.Sex) then
begin
FindIndex:=I;
Break;
end;
end;
if FindIndex>-1 then
UserList.Delete(FindIndex);
end;
 

Similar threads

S
回复
0
查看
896
SUNSTONE的Delphi笔记
S
S
回复
0
查看
873
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部