Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write a class to disk?

Status
Not open for further replies.

forcers

Programmer
Jun 16, 2004
3
US
I have my own class defined. It contains some primatives, but also a couple of methods and some arrays of DynamicArrays (e.g., DynamicArray<AnsiString>[10]).

I populate this class at startup from an ini file (a large ini file) via TIniFile. Once populated, I want to be able to store the class, intact, to disk. This way, the next time I startup, I can simply read the stored class and not have to parse the enormous ini file (it takes a long time). I know that the ini file was a poor choice for this, but I'm stuck with it and I somewhat regret it now.

I've experimented a little, but with no success. I've tried:

1) Using TStream (or similar, code not in front of me), but the length of bytes to write is required. I do a sizeof and get only 10 or some small number (I guess the size of the primatives and pointers in my class).

2) I extended the TObject class, which gave me more methods, one of them ->InstanceSize, but that return only 60 (I know the size to at least 200,000).

How do I wrate a class to disk and restore it at a later time?

Thanks in advance.
 
Howdy,

I dont understand why you create all these object(s) dynamically. It seems like you are asking that you want to create them dynamically once, then store that entire object and use it over and over again. Why not hard code the data into the object. You will certainly then be able to restore those values later.

So, basically, why exactly do you have these dynamica arrays? Is there something I am misunderstanding about your project?

It does sound like hard coding all that data may take a while, but I dont know of any other recourse...

Good luck,
onrdbandit

No! Try not. Do, or do not. There is no try. - Yoda
 
The data is customizable, so hard-coding is not an option. I do the customization via and form and maintain the ini file. When the user changes it, I need to read it in, and then store it as a disk object.

Think of the ini file as a source code and the populated classes as the "compiled version" of the ini file source.

I'm not disputing that I made a very poor design choice from the get go; that is, choosing ini as the storatge device for my data. I had no idea that reading a 1,600 line ini file would be so slow. This is my first Windows program.

The solution, though, is a decent one -- if I figure out how to store a class, intact, to disk. I could then simply read the entire class (or object, if you will) and I'm set.
 
as far as I remember there exists a class TPersistent. Extend that class.

Ion Filipski
1c.bmp
 
Howdy,

Ok, I get what you are trying to do. A few quick questions:

1) Is there any way you can optimize the INI processing, specifically in formatting the file
2) Are all the values loaded from the INI used immediately? If not maybe you can break the ini into multiple components and only load what you need when you need it.
3) If you are using an array of AnsiStrings, a better way to do this, may be to use a TStringList object. Then you can load the file w/ the StringListObjectName->LoadFromFile method. I think this will improve the speed of the loading process. (In a test project I can load 1600 30 char lines in a blink...)

These are just a few suggestions. I have no idea about the saving the class part, but I am the King of superglue and duct tape work-arounds [thumbsup2]

One last note... the TStringList object has a SaveToStream method. That may help you w/ saving the class...

Good luck,
onrdbandit

No! Try not. Do, or do not. There is no try. - Yoda
 
Excellent suggestions.

IonFilipski, I'll look into the TPersistent first.

onrdbandit, I especially like number 3. I could definately have parallell TStringLists and save them to disk as my "compiled version" of the ini. Fortunately, my data structure is something like:

Data01 = fish
AltA01 = shark
AltB01 = trout
Rule01 = water

Data02 = canine
AltA02 = Rat Terrier
AltB02 = Boxer
Rule02 = whatever

Where each thing I need to store always has the same number of parameters. I'm a Java programmer, and its ArrayList class is almost exactly like TStringList.

I found something very close to what I was originally looking for here: . It is written in Delphi, but C++ Builder seems to share a lot with that language. If you would, perhaps peek at it -- it's a little over my head at first glance, but I think it is applicable -- there is source attached (quite small and could be scanned quickly).
 
Howdy,

Hope the TStringList helps. From reading the article you posted, it seems like it can do exactly what you need...

I assume your .ini file looks exactly like this:

Data01 = fish
AltA01 = shark
AltB01 = trout
Rule01 = water

Data02 = canine
AltA02 = Rat Terrier
AltB02 = Boxer
Rule02 = whatever


But if so, my question is, why include the "Data01 = " and "AltA01 = " portions. Rather I would just list the data like so:

fish
shark
trout
water


Since you know that there are always for data entries per set. You can just parse the list and place each entry into its appropriate place when processing the list.

I think you will beat this one soon, so good luck!

onrdbandit

No! Try not. Do, or do not. There is no try. - Yoda
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top