Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to save and load a TList to / from a stream Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
Others
Language
Delphi 2.x
Views
58
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			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;




			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC