Author: Jonas Bilinkevicius
TStrings.LoadFromStream is assuming that itself consumes the rest of the stream!
What if I want to save 2 stringlists to the same stream?
Answer:
That is as designed. Just use an intermediate stream to accomplish what you want.
Or you can take the following approach using a string buffer:
1 { ... }2 var3 list1: TStringList;
4 list2: TStringList;
5 lng: cardinal;
6 stream: TMemoryStream;
7 tmpS: string;
8 begin9 list1 := TStringList.Create;
10 list2 := TStringList.Create;
11 try12 stream := TMemoryStream.Create;
13 try14 {Assume there was code to get something into stream. 15 The layout of the stream is:16 size1|block1|size2|block2}17 {Read size of the 1st block}18 stream.read(lng, SizeOf(lng));
19 if lng > 0 then20 begin21 {if there are contents, read the block to tmpS}22 SetLength(tmpS, lng);
23 stream.read(tmpS[1], lng)
24 {Assign tmpS to the Text property of list1}25 list1.Text := tmpS;
26 end;
27 {Same procedure for list2}28 stream.read(lng, SizeOf(lng));
29 if lng > 0 then30 begin31 SetLength(tmpS, lng);
32 stream.read(tmpS[1], lng)
33 list2.Text := tmpS;
34 end;
35 finally36 end;
37 finally38 list2.Free;
39 list1.Free;
40 end;
41 end;