我在写一个RSA的加密单元,需要保存多个特殊类型变量的值为一个文件(无法按照String等方式保存)。应该使用流,开始我定义tom为记录类型(里面包含多个特殊类型变量),但发现无法写入流,后来定义为一个继承自TComponent的类,WriteComponent(),能够运行,但一从保存的文件读取时就出错,后来我经过反复研究,认为tom实例中的变量值并未在保存时写入文件tom.dat中。下面是我简化的程序,我学delphi时间短,查阅很多大富翁以前的帖子,也找不到原因,请各位指点一下到底该怎么写,谢谢!
==========================
uses  FGInt, FGIntPrimeGeneration, FGIntRSA, ranlib;
type  tom = class(TComponent)   //实际的tom包含多个特殊类型的变量,这里是简化
      public
      aa: TFGInt ;
      end;
var  a: TFGInt;
implementation
{$R *.dfm}//保存
procedure TForm1.Button1Click(Sender: TObject);
var   tom1 : tom ;
      ss: TfileStream ;
begin
// 运算,初始化变量a,具体程序省略 //
   tom1:= tom.Create(self) ;
   tom1.aa:=a ;
   try
   ss:= TfileStream.Create(ExtractFilePath(Application.ExeName)+'tom.dat',fmOpenWrite or fmCreate) ;
   ss.WriteComponent(tom1);
   finally
   ss.Free ;
   end ;
   tom1.Free ;
end;//读取并验证部分值
procedure TForm1.Button2Click(Sender: TObject);
var  kk: string ;
     tom2 : tom ;
     ww: TfileStream ;
begin
   tom2:=tom.Create(self);
   try
   ww:= TfileStream.Create(ExtractFilePath(Application.ExeName)+'Initi.dat',fmOpenRead) ;
   ww.ReadComponent(tom2) ;
   finally
   ww.Free ;
   end ;
   FGIntToBase10String(tom2.aa, kk) ;
   label1.caption:=kk ;
   tom2.Free ;
end;
=====================================
其中,TFGInt和TSign的定义:
type  TFGInt = Record
        Sign : TSign;
        Number : Array Of LongWord;
      End;
      TSign = (negative, positive);

解决方案 »

  1.   

    没仔细看, 有一个地方一定会有问题,就是number:array of longword,
    是一个动态数组,number是一个指针,缓冲区是临时申请的,所以,写流的
    时候是不能把number指针指向的内容写入的。改成静态数组。
      

  2.   

    谢谢楼上的,但是变量类型TFGInt是别人定义的,改的话要涉及整个FGInt.pas,几乎不可能。
      

  3.   

    我现在已经想办法把动态数组转化为自定义的静态数组,但是自定义的静态数组的值还是不能成功写入dat文件。