Jump to content
xisto Community
Sign in to follow this  
overture

Delphi - Tfilestream Object Persistence... use TFileStream

Recommended Posts

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

 

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. :P

 

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 listboxGameName.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:

 

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.

Edited by NilsC (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Storing results in a variableDelphi - Tfilestream

I'm working on a delphi appllication,I'm using this

with TOracleQuery.Create(nil) do begin   Session := OracleSession1;   Close ;   SQL.Clear ;   Iresults := SQL.Add('select (max(user_id)+1) as max_user_id ');   SQL.Add('from scs_users');

if tblUser.RecordCount <= 0 then begin   if edtSCSUser.Text <> '' then   begin TblUser.Insert; TblUser.FieldByName('USER_NAME').AsString   := UpperCase((edtSCSUser.Text)); TblUser.FieldByName('USER_LEVEL').AsString := Trim(cmbSCSAccess.Value); TblUser.FieldByName('password').AsString := DoEncrypt('AMCOAL1','PWD'); TblUser.FieldByName('password_date').AsDateTime := Now; TblUser.FieldByName('USER_ID').AsInteger := Iresults; TblUser.FieldByName('SCS_ACCESS').AsString := Trim(cmbSCS.Text); TblUser.FieldByName('DH_ACCESS').AsString  := Trim(cmbDH.Text); TblUser.Post; messagedlg('SCS User '+ trim(edtSCSUser.Text)+ 'created successfully', mtInformation, [mbok], 0) ;   end

but the query results are not being updated correctly in the users table, when I run the query in the sqlplus I return correct results but when I try to do the same on the delphi program its not returning the correct result.The system increment the user_id value each time a new user is being created so now I'm trying to carry out the same functionality using the delphi application please assist

-reply by Nomsa

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.