我在Delphi 2009中有以下结构:
type
IndiReportIndi = record
IndiName: string;
NameNum: integer;
ReportIndiName: string;
end;
var
XRefList: array of IndiReportIndi;
其中XRefList是动态数组。
我想将XRefList保存到FileStream。我该怎么做并包括所有IndiName和ReportIndiName字符串,以便在我稍后从该FileStream加载时它们都可以再次检索?
答案 0 :(得分:10)
type
IndiReportIndi = record
IndiName: string;
NameNum: integer;
ReportIndiName: string;
procedure SaveToStream(Stream: TStream);
procedure LoadFromStream(Stream: TStream);
end;
type
TXRefList = array of IndiReportIndi;
function LoadString(Stream: TStream): string;
var
N: Integer;
begin
Result:= '';
Stream.ReadBuffer(N, SizeOf(Integer));
if N > 0 then begin
SetLength(Result, N);
// Stream.ReadBuffer(Result[1], N * SizeOf(Char));
// fast version - see comment by A.Bouchez
Stream.ReadBuffer(Pointer(Result)^, N * SizeOf(Char));
end;
end;
procedure SaveString(Stream: TStream; const S: string);
var
N: Integer;
begin
N:= Length(S);
Stream.WriteBuffer(N, SizeOf(Integer));
if N > 0 then
// Stream.WriteBuffer(S[1], N * SizeOf(Char));
// fast version - see comment by A.Bouchez
Stream.WriteBuffer(Pointer(S)^, N * SizeOf(Char));
end;
procedure IndiReportIndi.LoadFromStream(Stream: TStream);
var
S: string;
begin
IndiName:= LoadString(Stream);
Stream.ReadBuffer(NameNum, SizeOf(Integer));
ReportIndiName:= LoadString(Stream);
end;
procedure IndiReportIndi.SaveToStream(Stream: TStream);
begin
SaveString(Stream, IndiName);
Stream.WriteBuffer(NameNum, SizeOf(Integer));
SaveString(Stream, ReportIndiName);
end;
function LoadXRefList(Stream: TStream): TXRefList;
var
N: Integer;
I: Integer;
begin
Stream.ReadBuffer(N, SizeOf(Integer));
if N <= 0 then Result:= nil
else begin
SetLength(Result, N);
for I:= 0 to N - 1 do
Result[I].LoadFromStream(Stream);
end;
end;
procedure SaveXRefList(Stream: TStream; const List: TXRefList);
var
N: Integer;
I: Integer;
begin
N:= Length(List);
Stream.WriteBuffer(N, SizeOf(Integer));
for I:= 0 to N - 1 do
List[I].SaveToStream(Stream);
end;
答案 1 :(得分:6)
使用:http://code.google.com/p/kblib/
type
IndiReportIndi = record
IndiName: string;
NameNum: integer;
ReportIndiName: string;
end;
TXRefList = array of IndiReportIndi;
var
XRefList: TXRefList;
要将整个XRefList保存为流使用:
TKBDynamic.WriteTo(lStream, XRefList, TypeInfo(TXRefList));
要加载它:
TKBDynamic.ReadFrom(lStream, XRefList, TypeInfo(TXRefList));
答案 2 :(得分:5)
var
S: TStream;
W: TWriter;
A: array of IndiReportIndi;
E: IndiReportIndi;
...
begin
S := nil;
W := nil;
try
S := TFileStream.Create(...);
W := TWriter.Create(S);
W.WriteInteger(Length(A));
for E in A do
begin
W.WriteString(E.IndiName);
W.WriteInteger(E.NameNum);
W.WriteString(E.ReportIndiName);
end;
finally
W.Free;
S.Free
end;
end;
要读取这些数据,请使用TReader和ReadInteger / ReadString。
答案 3 :(得分:1)
答案 4 :(得分:0)
只是为了准确答案:
考虑查看TDynArray
包装器,它能够将任何记录序列化为二进制文件,也可以将动态数组序列化。
有很多类似TList
的方法,包括新方法(如散列或二进制搜索)。
针对速度和磁盘使用进行了优化,适用于Delphi 5到XE2 - 以及开源。
答案 5 :(得分:0)
经过测试的解决方案:
procedure TForm1.FormCreate(Sender: TObject);
VAR Stream: TFileStream;
R: IndyRecordIndy;
begin
Stream:= TFileStream.Create;
TRY
//put rec in stream
Stream.WriteBuffer(R, SizeOf(R));
//restore record from stram
Stream.Position := 0;
Stream.ReadBuffer(R, Stream.Size);
FINALLY
FreeAndNil(Stream);
END;
end
;