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;

解决方案 »

  1.   

    定义成文件类型,不能用超过255的string,只能用shortstring;
    所以  body shortstring;或body string[255]
      

  2.   

    TO  laihecongxi(兴哥), 定义成Ansistring 编译在
    var
    Files: File of TMydatafile; 这里通不过
      

  3.   

    string[i]的i要在1到255之间
      

  4.   

    接招:type
      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;
      

  5.   

    hardnut() : 不会吧, 你定义数组也是固定了的啊。 超长了怎么办?
      

  6.   

    看看这样可以吗:
       body:^string;
    应用的时候:
    var
      Mydatafile: Tmydatafile;
      Files: File of TMydatafile;
    begin
      new(MyDataFile.body) ;
      MyDataFile.body^:='ssssss';
      showmessage(MyDataFile.body^);
      dispose(MyDataFile.body);
      

  7.   

    这确实是一个大难题,本人的一个软件现在只好用数据库了,如果将它定义成
    char的话,在处理中文方面或双字节字符方面就会有问题;本人一直无法解决
      

  8.   

    lincanwen(Too Two To) : 你的方法保存数据没问题,但是怎样从数据文件中提出body对应部分的内容呢?
      

  9.   

    kkkkk4388(罗): 如题, body要求存放长度不定的字符串! 怎样解决foreveryday007(foreveryday007) : 用String不行
      

  10.   

    使用字符数组就可以!!!定长 Array [  0 .. X ] Of Char;
    不定长 Array Of Char;
      

  11.   

    Delphi_Li(Delphi Li): 使用字符数组 就不能如下定义:                    var   Files: File of TMydatafile;
      

  12.   

    unit Unit1;interfaceuses
      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.
      

  13.   

    用string类型,或PChar, Pointer类型,至于怎么转,看看classes.TStringStream是怎么实现一个string保留all info...
      

  14.   

    genphone_ru(改行去学VC):  长度要是超过1000呢?? 内容就不完整了!
      

  15.   

    你就用STRING就行了,编译开关用缺省的$H应该可以的!
      

  16.   

    to god263(屁包):你不是说是"定长"的吗?
    如果真是不定长的,用一个类吧:
     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)它的保存与传递(在不同进程,不同机器中(通过网络))时会有不少麻烦
      

  17.   

    genphone_ru(改行去学VC):  长度要是超过1000呢?? 内容就不完整了!const MaxLength = 99999999; //设置成你所需要的最大值不就可以了?
      

  18.   

    嗯,差不多是这样,将楼上的TStream.Read/Write改为: ReadBuffer/WriteBuffer就行了。不然读写出错了你都不知道
      

  19.   

    genphone_ru:
      这种情况不应该使用静态方法,不然数据会N多冗余,可以使用string,pchar之类的类型保存地址,然后将长度写入,再将内容写入,读时就是反操作
      

  20.   

    我需要存为类型文件,这样在读取数据的时候很方便! 所以我才用
    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;
      

  21.   

    用指针可以很方便的将数据写到类型文件里, 但是读数据的时候就不好办了(就Body不好读,指针指不到)
      

  22.   

    晕倒, hardnut()快来,再写个类来完成他要的功能。那类已经写的很明白了,如果不明白,你应该问问
      

  23.   

    另外开贴送分! :)
    http://expert.csdn.net/Expert/topic/1360/1360834.xml?temp=.6661798