Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Delphi - Tfilestream, Object Persistence... use TFileStream
overture
post Feb 14 2005, 04:01 PM
Post #1


Premium Member
Group Icon

Group: Members
Posts: 208
Joined: 6-September 04
From: England
Member No.: 315



OK. This tutorial may be helpful to people who use Delphi, for anyone else... it will probably be boring smile.gif.

This tutorial is going to explain briefly how to use the TFileStream Class.

For anyone who has wanted to say... save any data from a listbox and during run-time you wanted to append different information to it, or throughout the applications running time and then save the information when the application closes then you should use the TFileSteam Class. The great thing about this is it can read, write and save the information in the exact order from any component (in this case a listbox). When using it, it writes and saves the data onto the hard-disk in some kind of file - this file type can be chosen during codeing.

When you want to reload the data previously saved all it has to do is stream it back into the memory and specify it to a component. Remember when it is reloaded it does so in its original form (order) which is the whole point of using it in this case.

note: i am showing this code from within a procedure so you need to read on to see how it is called. biggrin.gif

The reason i want to share the following information i recently had to create it for a programming assignment for college, and instead of me iterating the code i decided to put it all into a single procedure. The scenario was that i needed to create a game in which match fixtures were created and it required me to keep each set of fixtures for future reference(there was a whole lot more to it but it doesn't matter).

This is the basic code which is needed to read the data into the listbox is as follows (i have added comments to explain briefly what each part does)
CODE
GameName:=TFileStream.Create(FileName, fmOpenRead); //It creates the stream from the specified filename. It then opens and reads the appropriate file using the 'fmOpenRead' mode. There are many more values to suit wider needs.
GameName.ReadComponent(lb); //read the items into the listbox in the correct order (done automatically)
GameName.Free; //free the memory which this process has taken up


note: 'lb' stands for 'ListBox'

This next block of code is to show you how to save the data:
CODE
GameName:=TFileStream.Create(FileName, fmCreate); //creates the file using the 'fmCreate' mode.
GameName.WriteComponent(lb); //This gathers the data from the specified listbox
GameName.Free;


This is the basis for reading, writing and saving data from the listbox. The problem i had was i needed to do this several times for different games and this may have took some time. Instead i decided to make a procedure to perform this for me and me only specifying the FILENAME and GAMENAME.

This is what the whole procedure looks like:
CODE
PROCEDURE TfrmMain.LBFileStream(GameName:TFileStream; FileName:String);
BEGIN
 IF FileExists(FileName) THEN
   BEGIN
     GameName:=TFileStream.Create(FileName, fmOpenRead);
     GameName.ReadComponent(lb);
     GameName.Free;
   END;
 GameName:=TFileStream.Create(FileName, fmCreate);
 GameName.WriteComponent(lb);
 GameName.Free;
END;


As you can see i have added a little more checking in the code, i have checked to see whether the file exists or not - if it does then to read the items into the listbox, and if not then to read the listbox and create the appropriate file.

There are two variables which i have included within this procedure - GameName (which is what the file game will be called, and the filename which is what file will be called.

The last thing to do now is to call the procedure to create and read our file. This is the code:

CODE
LBFileStream(SpillikinsStream, 'SpillikinsList.dat');


The word Spillikins refers to a type of Game. As you can see the two variables which were declared within the procedure have been used (the ones within the brackets). I decided i wanted to store the items into a .DAT file as to keep the items as secure as possible (.DAT files are not easily editable). A file will be created called 'SpillikinsList.dat'. This will hold the data from the listbox.

You see that the one line of code can be used to call the single procedure multiple times, this saves me writing out the code each time for each game.

I hope this has helped some of you if not informed you of something you might not otherwise have known.

If you see a problem or mistakes please tell me and i will correct them as i am doing this and a lot of other work at the same time. thankyou

overture.

This post has been edited by NilsC: Feb 14 2005, 05:51 PM
Go to the top of the page
 
+Quote Post
iGuest
post Jun 25 2008, 08:36 AM
Post #2


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



improvement of the example
Delphi - Tfilestream

Basic but useful (for who does not know about it).

The parameter "(GameName:TFileStream;" is useless. Use a local var instead.

(or it's a typo and you wanted to pass the listbox to save.)



-reply by Loda
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics


 



- Lo-Fi Version Time is now: 14th October 2008 - 01:43 AM