2012-01-27 39 views
3

請有人可以幫助我在拯救,並從流加載其動態數組德爾福保存和加載動態數組

const 
     iGlobHolderCount = 100; 

    type 
     TFiLeSpec = record 
     iSize: Integer; 
     end; 

     TFileSpecLst = array of TFiLeSpec; 

     TFiLeSpecList = record 
     iMin: Integer; 
     iMax: Integer; 
     iCount: Integer; 
     FileSpecLst: TFileSpecLst; 
     end; 


var 
FFileSpec: array of TFiLeSpec; 

FFileSpecList: array [1 .. iGlobHolderCount] of TFiLeSpecList; 
+1

你試過了什麼?它現在看起來像是一個「Show me teh codez」問題。 – 2012-01-27 11:00:31

+0

您是否嘗試過在我的回答中顯示的技術,您的其他問題(http://stackoverflow.com/questions/8819885/delphi-save-and-load-binary-tree)或變體在您的其他問題的第一個評論中提出的建議題? (http://stackoverflow.com/questions/3820996/delphi-2010-how-to-save-a-whole-record-to-a-file)。你應該嘗試自己解決這個問題併發布失敗的代碼。沒有這一點,它確實是一個「顯示我的代碼」問題。投票結束爲「太局部」,因爲這隻會幫助你,而不是其他人。 – 2012-01-27 11:05:40

+0

@Cosmin Prund,不,它不是顯示我的codez,但只是說實話 我不知道如何保存和加載流動態數組。 – Alexis 2012-01-27 11:06:05

回答

4

寫入陣列的第一長度,和旁邊的陣列數據:

type 
    TItem = Integer; 
    TItemArray = array of TItem; 

var 
    Stream: TStream; 
    Arr: TItemArray; 
    L: LongWord; 

begin 
    Arr:= TItemArray.Create(1, 2, 3); 
// To save 
    Stream:= TFileStream.Create('C:\Temp\test.bin', fmCreate); 
    L:= Length(Arr); 
    Stream.WriteBuffer(L, SizeOf(L)); 
    Stream.WriteBuffer(Pointer(Arr)^, L * SizeOf(TItem)); 
    Stream.Free; 
// To load 
    Stream:= TFileStream.Create('C:\Temp\test.bin', fmOpenRead); 
    Stream.ReadBuffer(L, SizeOf(L)); 
    SetLength(Arr, L); 
    Stream.ReadBuffer(Pointer(Arr)^, L * SizeOf(TItem)); 
    Stream.Free; 
end; 
5

從Delphi 5直到XE2的另一個解決方案是使用我們核心OpenSource單元的一些功能。

事實上,它實現了:

  • 處理記錄類型的一些低級別RTTI功能:RecordEquals,RecordSave,RecordSaveLength,RecordLoad;
  • 一個專用的TDynArray對象,它是任何動態數組的一個包裝,能夠暴露任何動態數組周圍的TList類方法,甚至包含記錄,字符串或其他動態數組。它能夠序列化任何動態數組。
  • 序列化使用優化的二進制格式,並且能夠將任何記錄或動態數組保存並加載爲RawByteString。

您可以編碼,例如

var 
    FFileSpec: array of TFiLeSpec; 
    TFileSpecList = array of TFiLeSpecList; 
    FFileSpecList: TFileSpecList; 

var FSL: TDynArray; 
    Bin: RawByteString; 
begin 
    FSL.Init(TypeInfo(TFiLeSpecList),FFileSpecList); 
    // ... then you use FFileSpecList[] as usual 
    // ... or use some methods of FSL: 
    if FSL.Count>0 then 
    FSL.Delete(0); 
    FSL.Add(FFileSpec); 
    FSL.Clear; 
    // then you can serialize the content to binary 
    Bin := FSL.SaveTo; 
    // use FSL.LoadFrom(Bin) to read the whole array content back 
    // or you can use a TStream 
    FSL.SaveToStream(aStream); 
    FSL.Clear; 
    aStream.Position := 0; 
    FSL.LoadFrom(aStream); 
    // you do not need to release nor Free FSL: this is a wrapper around FFileSpecList 
end; 

請注意,我取代你TFileSpecList通過動態數組,但你可以使用固定陣列代替,創內提供額外的RTTI - 然後使用RecordLoad/RecordSave功能。它將使用RTTI保存內部動態數組內容(即使使用Delphi 5),處理內部的任何string或嵌套數組。它被我們的mORMot框架(例如動態數組的serialization到DB中)使用,但它不是它的一部分:只需一個單元,也不需要SQLite3,也不需要整個ORM類。

參見this page for additional information

+0

http://synopse.info/files/html/Synopse%20mORMot%20Framework%20SAD%201.18.html#TITL_48 的更新文檔包含JSON序列化在內的許多新功能。 – 2015-06-05 09:42:41