风
风云再起
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
PStuStudent;
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;
PStuStudent;
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.
然后把他保存为文件
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
PStuStudent;
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;
PStuStudent;
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.