Author: Tomas Rutkauskas Can anyone tell me how to stream a TList to a file? Given: 1 type 2 PassWordItem = class(TObject) 3 accountName: string[30]; 4 userName: string[25]; 5 passWd: string[10]; 6 end; 7 8 and... 9 10 PassWdList: TList; 11 12 {How do I stream the contents of PassWdList to a file, and subsequently load TList 13 from the file? 14 15 Answer: 16 17 Lets add a couple of methods to the PassWordItem class:} 18 19 20 PassWordItem = class(TObject) 21 public 22 accountName: string[30]; 23 userName: string[25]; 24 passWd: string[10]; 25 26 procedure SaveToStream(s: TStream); 27 constructor CreatefromStream(S: TStream); 28 end; 29 30 31 //With that you can write two procedures (or methods of a TPasswordList class 32 derived from TList): 33 34 35 procedure SavePasswordlist(pwl: TLIst; S: TStream); 36 var 37 i: Integer; 38 begin 39 Assert(Assigned(pwl)); 40 Assert(Assigned(S)); 41 i := pwl.count; 42 S.write(i, sizeof(i)); 43 for i := 0 to pwl.count - 1 do 44 PasswordItem(pwl[i]).SaveToStream(S); 45 end; 46 47 procedure LoadPasswordList(pwl: TList; S: TStream); 48 var 49 count, n: Integer; 50 begin 51 Assert(Assigned(pwl)); 52 Assert(Assigned(S)); 53 S.read(count, sizeof(count)); 54 pwl.Capacity := count; 55 for n := 1 to count do 56 pwl.Add(PasswordItem.CreatefromStream(S)); 57 end; 58 59 procedure Passworditem.SaveToStream(s: TStream); 60 begin 61 Assert(Assigned(S)); 62 S.write(accountname, Sizeof(accountname)); 63 S.write(username, sizeof(username)); 64 S.write(passwd, sizeof(passwd)); 65 end; 66 67 68 constructor CreatefromStream(S: TStream); 69 begin 70 Assert(Assigned(S)); 71 inherited Create; 72 S.read(accountname, Sizeof(accountname)); 73 S.read(username, sizeof(username)); 74 S.read(passwd, sizeof(passwd)); 75 end;