我定义了一个类型:
type person=record
  a:integer;
  b:string;
  c:tjpegimage;
  d:tdatetime;
end;
现在程序中有一个该类型的实例P,我的问题是,如何将p利用filestream保存成一个二进制文件。
希望各位高人能指点指点!谢谢

解决方案 »

  1.   

    用TFileStream的WriteBuffer函数。
    fstrm:TFileStream;
    fstrm.WriteBuffer(@P,sizeof(p));
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      person=record
        a:integer;
        b:string;
        c:Double;
        d:tdatetime;
      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}procedure TForm1.Button1Click(Sender: TObject);//保存
    var
      s:TFileStream;
      p:Person;
    begin
      p.a:=10;
      p.b:='test';
      p.c:=12.34567;
      p.d:=StrtoDateTime('2002-01-01');  s:=TFileStream.Create('c:\t.bin',fmOpenReadWrite or fmCreate);
      try
        s.Write(p,Sizeof(Person));
      finally
        s.Free;
      end;
    end;procedure TForm1.Button2Click(Sender: TObject);//读取
    var
      s:TFileStream;
      p:Person;
    begin
      s:=TFileStream.Create('c:\t.bin',fmOpenRead);
      try
        s.Read(p,Sizeof(Person));
      finally
        s.Free;
      end;
      ShowMessage('如果显示的数据和保存过程中的数据一样,说明FileStream保存成功'#13+
                  IntToStr(p.a)+#13+
                  p.b+#13+
                  FloatToStr(p.c)+#13+
                  FormatDateTime('yyyy-mm-dd',p.d));
    end;end.
      

  3.   

    贴一段代码给你,你改改吧
    unit ceshi;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        OpenDialog1: TOpenDialog;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);  private
        { Private declarations }
      public
        { Public declarations }
      end;
    type
    //枚举
    mymj=(a,b,c,d,e);
    //集合
    myjh=set of mymj;
    //静态数组
    mysz=array[1..10] of integer;
    //子界
    myzj='a'..'z';
    //记录
    myrecord=record
             name:array[1..10] of char;
             age:0..200;
             sex:(gril,boy);
             filed1:mymj;
             filed2:myjh;
             filed3:mysz;
             filed4:myzj;
             case tag:boolean of
                  true:(filed5:integer;);
                  false:(filed6:string[100]);
             end;
    //文件
    myfile=file of myrecord;
    myfile_nottype=file;
    var
      Form1: TForm1;
      tmymj:mymj;
      tmyjh:myjh;
      tmysz:mysz;
      tmyzj:myzj;
      tmyrecord:myrecord;
      tmyfile:myfile;
      tmyfile_nottype:myfile_nottype;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    varFileName:string;begin
    tmyjh:=[a,c,e];
    tmysz[1]:=1982;
    tmysz[2]:=3;
    tmysz[3]:=20;
    if tmymj=a then  showmessage('符合条件《枚举》');
    if a in tmyjh then  showmessage('符合条件《集合》');
    if tmysz[3]= 20 then  showmessage('符合条件《数组》');tmyrecord.name :='wangyuntao';
    tmyrecord.age :=22;
    tmyrecord.sex :=boy;
    tmyrecord.filed1 :=a;
    tmyrecord.filed2 :=[a,b,c];
    tmyrecord.filed3[1]:=100;
    tmyrecord.filed4 :='h';
    tmyrecord.tag :=true;
    tmyrecord.filed5 :=99;
    tmyrecord.filed6 :='ok';if OpenDialog1.Execute then
    FileName := OpenDialog1.FileName
    else exit;AssignFile(tmyfile,Filename);
    Reset(tmyfile);
    //Write(tmyfile,tmyrecord);
    read(tmyfile,tmyrecord) ;
    showmessage(tmyrecord.name );
    showmessage(inttostr(tmyrecord.age));
    //子界
    showmessage(tmyrecord.filed4);end;
    procedure TForm1.Button2Click(Sender: TObject);
    var
    i:pointer;
    pi:pinteger;
    ki:pstring;
    ps:single;
    ka:ansistring;begin
    ps:=25869;
    ka:='i love delphi!';
    //取PS地址->I:POINTER
    i:=@ps;
    //将I强制转换成pi:pinteger
    pi:=pinteger(i);
    SHOWMESSAGE(INTTOSTR(PI^)) ;//取KA地址
    i:=@ka;
    ki:=pstring(i);
    showmessage(ki^);
    end;end.