type
TMyDataFile=Packed Record
aa: String[100];
bb: String[100];
body: ??; //怎样定义body,使他能存放长度不变的字符串(字符串很长)
end;
var
Mydatafile: Tmydatafile;
Files: File of TMydatafile;
begin
AssignFile(Files,'D:\Cmail.dat');
Reset(files);
Seek(Files,filesize(files));
Mydatafile.aa:= edit1.text;
Mydatafile.bb:= edit2.text;
Mydatafile.body := …………(如memo1.text) write(files,Mydatafile);
closefile(files);
end;
TMyDataFile=Packed Record
aa: String[100];
bb: String[100];
body: ??; //怎样定义body,使他能存放长度不变的字符串(字符串很长)
end;
var
Mydatafile: Tmydatafile;
Files: File of TMydatafile;
begin
AssignFile(Files,'D:\Cmail.dat');
Reset(files);
Seek(Files,filesize(files));
Mydatafile.aa:= edit1.text;
Mydatafile.bb:= edit2.text;
Mydatafile.body := …………(如memo1.text) write(files,Mydatafile);
closefile(files);
end;
所以 body shortstring;或body string[255]
var
Files: File of TMydatafile; 这里通不过
TMyDataFile=Packed Record
aa: String[100];
bb: String[100];
len:integer;
body: array [1..10000] of char;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t:TMyDataFile;
s,s2:string;
begin
//s:=Memo1.Text;
t.len:=Length(s);
CopyMemory(@t.body[1],@s[1],t.len); //保存数据
//do other
SetLength(s2,t.len);
CopyMemory(@s2[1],@t.body[1],t.len); //取得数据
end;
body:^string;
应用的时候:
var
Mydatafile: Tmydatafile;
Files: File of TMydatafile;
begin
new(MyDataFile.body) ;
MyDataFile.body^:='ssssss';
showmessage(MyDataFile.body^);
dispose(MyDataFile.body);
char的话,在处理中文方面或双字节字符方面就会有问题;本人一直无法解决
不定长 Array Of Char;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;const MaxCount = 1000;
type
TMyRecord = Record
str1: String[20];
str2: String[20];
body: array [0..MaxCount] of char;
end;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
Edit3: TEdit;
Edit4: TEdit;
Memo2: TMemo;
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 tfile : File of TMyRecord;
sFile: String;
myRecord: TMyRecord;
begin
sFile := 'd:\mytest.dat';
AssignFile(tfile,sFile);
if FileExists(sFile) then
Reset(tFile)
else
ReWrite(tFile);
Seek(tFile,FileSize(tFile));
myRecord.str1 := Edit1.Text;
myRecord.str2 := Edit2.Text;
StrCopy(myRecord.body,pChar(Memo1.Text));
Write(tFile,myRecord);
CloseFile(tFile);end;procedure TForm1.Button2Click(Sender: TObject);
var tfile : File of TMyRecord;
sFile: String;
myRecord: TMyRecord;
begin
sFile := 'd:\mytest.dat';
AssignFile(tfile,sFile);
if FileExists(sFile) then
Reset(tFile)
else
Exit;
while not Eof(tFile) do
begin
Read(tFile,myRecord);
Edit3.Text := myRecord.str1 ;
Edit4.Text := myRecord.str2 ;
Memo2.Text := myRecord.body ;
if Application.MessageBox('是否看下一个记录?','提示',MB_YESNO)= IDNO then
Break;
end;
CloseFile(tFile);
end;end.
如果真是不定长的,用一个类吧:
TMyData=class
public
aa:string;
bb:string;
body:string;
procedure SaveToStream(AStream:TStream);
procedure LoadFromStream(AStream:TStream);
end;implementationprocedure TMyData.LoadFromStream(AStream: TStream);
var
nLen:Cardinal;
begin
AStream.Read(nLen,SizeOf(nLen));
if nLen>0 then begin
SetLength(aa,nLen);
AStream.Read(aa[1],nLen);
end; AStream.Read(nLen,SizeOf(nLen));
if nLen>0 then begin
SetLength(bb,nLen);
AStream.Read(b[1],nLen);
end; AStream.Read(nLen,SizeOf(nLen));
if nLen>0 then begin
SetLength(body,nLen);
AStream.Read(body[1],nLen);
end;end;
procedure TMyData.SaveToStream(AStream: TStream);
var
nLen:Cardinal;
begin
nLen:=Length(aa);
AStream.Write(nLen,SizeOf(nLen));
if nLen>0 then AStream.Write(aa[1],nLen); nLen:=Length(bb);
AStream.Write(nLen,SizeOf(nLen));
if nLen>0 then AStream.Write(bb[1],nLen); nLen:=Length(body);
AStream.Write(nLen,SizeOf(nLen));
if nLen>0 then AStream.Write(body[1],nLen);end;
我不清楚你为为何喜欢用record,如果它的成员有的是指针类型(string,dynamic array,object)它的保存与传递(在不同进程,不同机器中(通过网络))时会有不少麻烦
这种情况不应该使用静态方法,不然数据会N多冗余,可以使用string,pchar之类的类型保存地址,然后将长度写入,再将内容写入,读时就是反操作
var
Mydatafile: Tmydatafile;
Files: File of TMydatafile;
begin
AssignFile(Files,'D:\Cmail.dat');
Reset(files);
Seek(Files,filesize(files));
Mydatafile.aa:= edit1.text;
Mydatafile.bb:= edit2.text;
Mydatafile.body := …………(如memo1.text) write(files,Mydatafile);
closefile(files);
end;
http://expert.csdn.net/Expert/topic/1360/1360834.xml?temp=.6661798