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 with records and TStrings objects 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
VCL-General
Language
Delphi 2.x
Views
97
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I know how to load and save following like records in a TList container with 
filestream, but how can I load and save records like this:

1   TClassRec = record
2     classname: string[10];
3     pupils: TStringlist; // which contains the ID-strings of each pupil
4   end;
5   
6   //Answer:
7   
8   //Pupils in an instance of the record above contains only a Pointer, the address of 
9   the actual object, and saving that is worse than useless. You need to add code to 
10  save the objects data. I would create a TWriter for the stream, that makes life a 
11  bit easier:
12  
13  
14  Stream := TFileStream.Create(Filename, fmCreate);
15  try
16    Writer := TWriter.Create(Stream, 4096);
17    try
18      writer.WriteInteger(Classlist.Count);
19      for I := 0 to ClassList.Count - 1 do
20      begin
21        ClassRec := ClassList[i];
22        Writer.WriteString(classrec^.classname);
23        Writer.WriteString(classrec^.pupils.text);
24      end;
25    finally
26      Writer.Free;
27    end;
28  finally
29    Stream.Free
30  end;
31  
32       
33  //The reading part needs to be modified accordingly, using a TReader in this case.
34  
35  
36  Stream := TFileStream.Create(Filename, fmOpenread or fmShareDenyWrite);
37  try
38    Reader := TReader.Create(Stream, 4096);
39    try
40      numRecords := Reader.readInteger;
41      for I := 1 to numRecords do
42      begin
43        New(classrec);
44        classrec^.classname := Reader.readString;
45        classrec^.pupils := TStringlist.Create;
46        classrec^.pupils.text := Reader.readString;
47        Classlist.Add(classrec);
48      end;
49    finally
50      Reader.Free;
51    end;
52  finally
53    Stream.Free
54  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