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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static Array/Read data from file into array

Status
Not open for further replies.

jgd12345

Technical User
Apr 22, 2003
124
GB
Hi, I have two issues I would like someone to help with. The first is I have an array:

type TPlant = class
EnglishName: String;
ScientificName: String;
Price: Double;
Toxic: String;
end;

and data is stored to it via procedure, what I would like is it tobe to access the data if I close and then re-open the program. I read something about a static array but wasn't too sure how to use it.

The 2nd issue is I have a file called (PlantData.txt) and I wish to read the data from the file into the same array mention above when the program is oppened. The text file is in the form showed bellow (I can change it to suite how you require). I tried the read procedures on delphibasics.co.uk but it was no help.

If anyone could help I'd be very greatful. Thanx

English Name 1 Scientific Name 1 1.8 poisonous
English Name 2 Scientific Name 2 2.4 non-poisonous
English Name 3 Scientific Name 3 2.3 poisonous
 
You have defined TPlant to be a class. A class is not the same as an array.

Can you show us the code of the procedure that stores data in to TPlant?

Andrew
 
Take a look at thread102-488753. It's not an exact fit to what you seem to be doing, but it may give you some ideas.
 
No such keyword as "static" in Pascal. We have class procedures and constants, which are what the word often describes. We do not have an inherent ability to stream objects to files (except for components).

You can write some simple code to stream and load items. Let's make TPlant a descendent of TCollectionItem just for fun. The TPlants collection knows how to open a file and the TPlant item knows how to read and write itself to a (file) stream.

This example writes the raw data to the file with only a length prefix to each string. This is not the most robust file format, however. Something like XML or even comma-delimited would be better. You can modify the TPlant to read and write a new format without changing the TPlants at all.

Code:
type 
TPlant = class(TCollectionItem)
public
  EnglishName:    String;
  ScientificName: String;
  Price:          Double;
  Toxic:          String;

  procedure LoadFromStream(AStream : TStream);
  procedure SaveToStream(AStream : TStream);
end;

TPlants = class(TCollection)
private
   function GetPlant(Index : Integer) : TPlant;
public
   property Items[Index : Integer] : TPlant read GetPlant;
   function Add : TPlant;
   procedure LoadFromFile(AFileName : String);
   procedure SaveToFile(AFileName : String);
end;
...
implementation
...
function TPlants.GetPlant(Index : Integer) : TPlant;
begin
   Result := inherited Items[Index] as TPlant;
end;

function TPlants.Add : TPlant;
begin
   Result := inherited Add as TPlant;
end;

procedure TPlants.LoadFromFile(AFileName : String);
var
   ThisStream : TFileStream;
begin
   Clear;        //  free all existing TPlants

   ThisStream := TFileStream.Create(AFileName, fmOpenRead);
   try
       while ThisStream.Postion < ThisStream.Size do    //  e.g. while not EOF
           Add.LoadFromStream(ThisStream);    // create a new item and load it
   finally
       ThisStream.Free;
   end;
end;

procedure TPlants.SaveToFile(AFileName : String);
var
   ThisStream : TFileStream;
   Counter : Integer;
begin
   ThisStream := TFileStream.Create(AFileName, fmCreate);   // overwrite if necessary
   try
       for Counter := 0 to Self.Count - 1 do
           Plants[Counter].SaveToStream(ThisStream);    // each item saves itself sequentially
   finally
       ThisStream.Free;
   end;
end;

procedure TPlant.LoadFromStream(AStream : TStream);
var
   ThisStringLength : Integer;
begin
   with AStream do
   begin
       Read(ThisStringLength, SizeOf(Integer));
       SetLength(EnglishName, ThisStringLength);
       Read(PChar(EnglishName)^, ThisStringLength);

       Read(ThisStringLength, SizeOf(Integer));
       SetLength(ScientificName, ThisStringLength);
       Read(PChar(ScientificName)^, ThisStringLength);

       Read(Price, SizeOf(Price));

       Read(ThisStringLength, SizeOf(Integer));
       SetLength(Toxic, ThisStringLength);
       Read(PChar(Toxic)^, ThisStringLength);
   end;
end;

procedure TPlant.SaveToStream(AStream : TStream);
begin
   with AStream do
   begin
       Write(Length(EnglishName), SizeOf(Integer));
       Write(PChar(EnglishName)^, Length(EnglishName));

       Write(Length(ScientificName), SizeOf(Integer));
       Write(PChar(ScientificName)^, Length(ScientificName));

       Write(Price, SizeOf(Price));

       Write(Length(Toxic), SizeOf(Integer));
       Write(PChar(Toxic)^, Length(Toxic));
   end;
end;


Cheers
 
Hi cheers I understand the second issue now but I still need some help with the first. It's probably quite easy but nothing I've used before.

Here's another example of the record

type TPerson = record
Name : string[20];
PhoneNo : String[20];
Age : real;
end;

var
Form1 : TForm1;
registeredPeople : array[1..1000] of Tperson;
numRegistered : integer; // Number of Registered People

And the line which creates/adds the data is:
registeredPeople[numRegistered] := person; // where person is the array ie person.Name is the value txtPerson.text passed from the form.

What I need is some way the array is stored so next time I open the program the data which was stored in the array previously will still be there.
 
&quot;For example,

type

PhoneEntry = record
FirstName, LastName: string[20];
PhoneNumber: string[15];
Listed: Boolean;
end;
PhoneList = file of PhoneEntry;

declares a file type for recording names and telephone numbers.
You can also use the file of ... construction directly in a variable declaration. For example,

var List1: file of PhoneEntry;

The word file by itself indicates an untyped file:

var DataFile: file;

For more information, see Untyped files.&quot;


From the Delphi Help



Steven van Els
SAvanEls@cq-link.sr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top