P
pangzhenguang
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyClass=class
public
a,b,c:String;
constructor Create;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass }
constructor TMyClass.Create;
begin
inherited Create;
a:='a';
b:='b';
c:='c';
end;
procedure TForm1.Button1Click(Sender: TObject);
var myclass:TMyClass;
TargetFile: TMemoryStream;
a,b,c:String;
begin
a:='a';
b:='b';
c:='c';
TargetFile := TMemoryStream.Create;
TargetFile.Write(a,SizeOf(a));
TargetFile.Write(b,SizeOf(b));
TargetFile.Write(c,SizeOf(c));
TargetFile.SaveToFile('d:/aaaa.dat') ;
TargetFile.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var TargetFile: TMemoryStream;
a,b,c:String;
begin
TargetFile := TMemoryStream.Create;
TargetFile.LoadFromFile('d:/aaaa.dat');
TargetFile.Read(a,SizeOf(a));
TargetFile.Read(b,SizeOf(b));
TargetFile.Read(c,SizeOf(c));
showmessage(a+';'+b+';'+c);
TargetFile.Free;
end;
end.
上面的代码读出的值没问题,为何下面的有问题呢:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyClass=class
public
a,b,c:String;
constructor Create;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass }
constructor TMyClass.Create;
begin
inherited Create;
a:='a';
b:='b';
c:='c';
end;
procedure TForm1.Button1Click(Sender: TObject);
var myclass:TMyClass;
TargetFile: TMemoryStream;
a,b,c:String;
begin
myclass:=TMyClass.Create;
TargetFile := TMemoryStream.Create;
TargetFile.Write(myclass.a,SizeOf(myclass.a));
TargetFile.Write(myclass.b,SizeOf(myclass.b));
TargetFile.Write(myclass.c,SizeOf(myclass.c));
TargetFile.SaveToFile('d:/aaaa.dat') ;
TargetFile.Free;
myclass.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var TargetFile: TMemoryStream;
a,b,c:String;
begin
TargetFile := TMemoryStream.Create;
TargetFile.LoadFromFile('d:/aaaa.dat');
TargetFile.Read(a,SizeOf(a));
TargetFile.Read(b,SizeOf(b));
TargetFile.Read(c,SizeOf(c));
showmessage(a+';'+b+';'+c);
TargetFile.Free;
end;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyClass=class
public
a,b,c:String;
constructor Create;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass }
constructor TMyClass.Create;
begin
inherited Create;
a:='a';
b:='b';
c:='c';
end;
procedure TForm1.Button1Click(Sender: TObject);
var myclass:TMyClass;
TargetFile: TMemoryStream;
a,b,c:String;
begin
a:='a';
b:='b';
c:='c';
TargetFile := TMemoryStream.Create;
TargetFile.Write(a,SizeOf(a));
TargetFile.Write(b,SizeOf(b));
TargetFile.Write(c,SizeOf(c));
TargetFile.SaveToFile('d:/aaaa.dat') ;
TargetFile.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var TargetFile: TMemoryStream;
a,b,c:String;
begin
TargetFile := TMemoryStream.Create;
TargetFile.LoadFromFile('d:/aaaa.dat');
TargetFile.Read(a,SizeOf(a));
TargetFile.Read(b,SizeOf(b));
TargetFile.Read(c,SizeOf(c));
showmessage(a+';'+b+';'+c);
TargetFile.Free;
end;
end.
上面的代码读出的值没问题,为何下面的有问题呢:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyClass=class
public
a,b,c:String;
constructor Create;
end;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TMyClass }
constructor TMyClass.Create;
begin
inherited Create;
a:='a';
b:='b';
c:='c';
end;
procedure TForm1.Button1Click(Sender: TObject);
var myclass:TMyClass;
TargetFile: TMemoryStream;
a,b,c:String;
begin
myclass:=TMyClass.Create;
TargetFile := TMemoryStream.Create;
TargetFile.Write(myclass.a,SizeOf(myclass.a));
TargetFile.Write(myclass.b,SizeOf(myclass.b));
TargetFile.Write(myclass.c,SizeOf(myclass.c));
TargetFile.SaveToFile('d:/aaaa.dat') ;
TargetFile.Free;
myclass.Free;
end;
procedure TForm1.Button2Click(Sender: TObject);
var TargetFile: TMemoryStream;
a,b,c:String;
begin
TargetFile := TMemoryStream.Create;
TargetFile.LoadFromFile('d:/aaaa.dat');
TargetFile.Read(a,SizeOf(a));
TargetFile.Read(b,SizeOf(b));
TargetFile.Read(c,SizeOf(c));
showmessage(a+';'+b+';'+c);
TargetFile.Free;
end;