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.
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)
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:
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:
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:
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
Edited by NilsC, 14 February 2005 - 05:51 PM.