结构化存储文件的保存和读取问题 (200分)

  • 主题发起人 主题发起人 bigchg
  • 开始时间 开始时间
B

bigchg

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure savetoFile中存入一个对象Dummy,其中包括record,TCollectionItems型字段,

procedure OpenFile中读取Dummy中的TCollectionItems对象可以,读取record型的数据为空,为什么?

保存://打开同级目录下的文件temp.fcd,存储dummy对象
if not Succeeded(stgOpenStorage('Temp.fcd',nil,
STGM_READWRITE or STGM_SHARE_EXCLUSIVE,NIL,0,stgTemp))then
OleCheck(StgCreateDocFile('Temp.fcd',
STGM_CREATE or STGM_READWRITE or STGM_SHARE_EXCLUSIVE,
0, stgTemp));

OleCheck(stgTemp.CreateStream('Lamp',
STGM_CREATE or STGM_WRITE or STGM_SHARE_EXCLUSIVE,
0,0, stm));

Dummy := TDummy.Create(nil);
try
Dummy.DX:= FDX;//record此处设断点测试有数据,FDX,FJGCSInItems为窗体的public成员
Dummy.JGCSInItems.Assign(FJGCSInItems);//TCollectionItems
OS:=TOleStream.Create(stm);
try
OS.WriteComponent(Dummy);
finally
OS.Free;
end;
finally
Dummy.Free;
end;
stgTemp:=nil;
stm:=nil;

打开://打开文件,读取对象dummy的数据
if not SUCCEEDED(StgOpenStorage('Temp.fcd', nil,
STGM_READWRITE OR STGM_SHARE_EXCLUSIVE,
nil, 0, stgTemp)) then messagebox(self.Handle,'打开文件错误!','提示',MB_OK);

if SUCCEEDED(stgTemp.OpenStream('Lamp', nil,
STGM_READ or STGM_SHARE_EXCLUSIVE, 0, stm)) then begin
Dummy := TDummy.Create(nil);
try
OS := TOleStream.Create(stm);
try
OS.ReadComponent(Dummy);
FDX:=Dummy.DX;
FJGCSInItems.Assign(Dummy.JGCSInItems);
finally
OS.Free;
end;
finally
Dummy.Free;
end;

procedure OpenFile中读取Dummy中的TCollectionItems对象可以,读取record型的数据为空,为什么?
 
可能不支持结构体数据,还是转换成对象吧,或者将结构体内的数据封装为对象的属性然后
使用桥模式来实现数据交换,我看了TTreeview的保存结构是从新写的,没有用writecomponent
 
我本来就封装成对象的属性了
type
TDummy = class(TComponent)
private
FDX: TDX;
FBLG: TBLG;
FDS: TDS;
FXZ: TXZ;
FQT: TQT;
FYGF: TYGF;
FG: TG;
FDT: TDT;
FDZF: TDZF;
FPrjAttrite: TPrjAttrite;
FJGCSInItems:TLampItems;
FJGCSOutItems:TLampItems;
FGZCSInItems:TLampItems;
FGZCSOutItems:TLampItems;
FBZ:TLampItems;
FBOMItems:TLampItems;
FSGBGItems:TLampItems;
FUIWItems:TUIWItems;
public
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
property DX: TDX read FDX write FDX;
property BLG: TBLG read FBLG write FBLG;
property DS: TDS read FDS write FDS;
property XZ: TXZ read FXZ write FXZ;
property QT: TQT read FQT write FQT;
property YGF: TYGF read FYGF write FYGF;
property G: TG read FG write FG;
property DT: TDT read FDT write FDT;
property DZF: TDZF read FDZF write FDZF;
property PrjAttrite:TPrjAttrite read FPrjAttrite write FPrjAttrite;
property JGCSInItems:TLampItems read FJGCSInItems write FJGCSInItems;
property JGCSOutItems:TLampItems read FJGCSOutItems write FJGCSOutItems;
property GZCSInItems:TLampItems read FGZCSInItems write FGZCSInItems;
property GZCSOutItems:TLampItems read FGZCSOutItems write FGZCSOutItems;
property BZ:TLampItems read FBZ write FBZ;
property BOMItems:TLampItems read FBOMItems write FBOMItems;
property SGBGItems:TLampItems read FSGBGItems write FSGBGItems;
property UIWItems:TUIWItems read FUIWItems write FUIWItems;
end;
 
我的意思是将DX结构体改为对象(属性published)
 
我上面的TLampItems是TCollectionItems型对象,其他的TDX,TBLG,....都是Record
 
接受答案了.
 
后退
顶部