X
xzmyzy
Unregistered / Unconfirmed
GUEST, unregistred user!
1.记录类型的结构
type
test = packed record
a: Boolean;
b: Integer;
c: string;
end;
ptest = ^test;
function SaveData(data:test
const filename: string): boolean;
var
Stream: TFileStream;
begin
Result := true;
try
Stream := TFileStream.Create(filename, fmCreate);
try
Stream.Position := 0;
Stream.WriteBuffer(data, SizeOf(data));
data.a:= true;
data.b:= 100;
data.c:= 'xzm';
Stream.WriteBuffer(data, SizeOf(data));
finally
Stream.Free;
end;
except
Result := false
{writing failed}
end;
end;
function LoadData(datatest
const filename: string): boolean;
var
Stream: TFileStream;
lCount: integer;
begin
Result := true
try
Stream := TFileStream.Create(filename, fmopenread);
try
Stream.Position := 0;
lCount:= Stream.Read(ldata^, SizeOf(ldata^));
lCount:= Stream.Read(ldata^, SizeOf(ldata^));
finally
Stream.Free;
end;
except
Result := false
end;
end;
2.将记录存入文件
procedure TForm1.Button7Click(Sender: TObject);
var a:test;
begin
a.b:=1;
a.c:= self.Edit1.text;//将Edit.text赋给a.c则不行,为什么? //'test';如果用字符串常量则可以
savedata(a,'c:/xzm.txt');
caption:=a.c;
end;
3.从文件中取记录数据
procedure TForm1.Button8Click(Sender: TObject);
var a:test;
begin
//new(a);
caption:= '';
loaddata(@a,'c:/xzm.txt');
caption:=a.c;
end;
以上代码中,a.c:= edit1.text存入不能正确读出,用a.c:='test'则可以,为什么?如何解决?
type
test = packed record
a: Boolean;
b: Integer;
c: string;
end;
ptest = ^test;
function SaveData(data:test
const filename: string): boolean;
var
Stream: TFileStream;
begin
Result := true;
try
Stream := TFileStream.Create(filename, fmCreate);
try
Stream.Position := 0;
Stream.WriteBuffer(data, SizeOf(data));
data.a:= true;
data.b:= 100;
data.c:= 'xzm';
Stream.WriteBuffer(data, SizeOf(data));
finally
Stream.Free;
end;
except
Result := false
{writing failed}
end;
end;
function LoadData(datatest
const filename: string): boolean;
var
Stream: TFileStream;
lCount: integer;
begin
Result := true
try
Stream := TFileStream.Create(filename, fmopenread);
try
Stream.Position := 0;
lCount:= Stream.Read(ldata^, SizeOf(ldata^));
lCount:= Stream.Read(ldata^, SizeOf(ldata^));
finally
Stream.Free;
end;
except
Result := false
end;
end;
2.将记录存入文件
procedure TForm1.Button7Click(Sender: TObject);
var a:test;
begin
a.b:=1;
a.c:= self.Edit1.text;//将Edit.text赋给a.c则不行,为什么? //'test';如果用字符串常量则可以
savedata(a,'c:/xzm.txt');
caption:=a.c;
end;
3.从文件中取记录数据
procedure TForm1.Button8Click(Sender: TObject);
var a:test;
begin
//new(a);
caption:= '';
loaddata(@a,'c:/xzm.txt');
caption:=a.c;
end;
以上代码中,a.c:= edit1.text存入不能正确读出,用a.c:='test'则可以,为什么?如何解决?