?
Solved

create/update/reading a file

Posted on 2003-03-17
11
Medium Priority
?
137 Views
Last Modified: 2010-04-04
Hi,

I creating a little app that analysis stock.  I want the user to be able to select preferences that won't change for the, i.e. TaxRate, ImputationCreditRate and MarginLendingRate .

I have a form that contains 3 fields for input and a button that captures these inputs.   I want this information to be saved in a file say 'C:\temp\ShareUser.dat'.  

At startup this file is read and each bit of data is stored in corresponding variables to be used in later calculations.  This file will only be created if the user actually go into Preferences form and changes values.

I quessing each field would be stored in either a line or comma dilimited format and a routine is required to update the appropriate field and read the appropriate field?

Need more info, just ask.  Help!  I'm new to this.  :-(

Thanks.
0
Comment
Question by:begonz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 

Accepted Solution

by:
RocketJockey earned 340 total points
ID: 8155818
Use the TINIFile (which is basically a text file anyway):

{******************************************
Used to create and populate the INI File
with default values if it doesn't already
exist
*****************************************}
procedure TForm2.FormCreate(Sender: TObject);
Var
  MyIniFile: TIniFile;
Begin
  //create the INI file
  MyIniFile := TIniFile.Create('C:\temp\ShareUser.ini');
 
  //set the TaxRate editbox
  edtTaxRate.text := MyIniFile.ReadString('TaxPrefs', 'TaxRate', 'Default TaxRate');
  //set the ImputationCreditRate editbox
  edtICRate.text := MyIniFile.ReadString('TaxPrefs', 'ICRate', 'Default ICRate');
  //set the MarginLendingRate editbox
  edtMLRate.text := MyIniFile.ReadString('TaxPrefs', 'MLRate', 'Default MLRate');
 
  MyIniFile.Free;
End;

{******************************************
Used to read and populate the form with
the values of the INI file. Uses the
default values if no entry in INI File is
found.
*****************************************}
procedure TForm2.Button1Click(Sender: TObject);
Var
  MyIniFile: TIniFile;
Begin
  //create the INI file
  MyIniFile := TIniFile.Create('C:\temp\ShareUser.ini');

  //read the TaxRate editbox
  MyIniFile.WriteString('TaxPrefs', 'TaxRate', edtTaxRate.text);
  //read the ImputationCreditRate editbox
  MyIniFile.WriteString('TaxPrefs', 'ICRate', edtICRate.text);
  //read the MarginLendingRate editbox
  MyIniFile.WriteString('TaxPrefs', 'MLRate', edtMLRate.text);
 
  MyIniFile.Free;

End;



I hope this helps
0
 

Expert Comment

by:RocketJockey
ID: 8155826
Oops. My comment blocks are reversed. Hope that didin't confuse you too much.
0
 

Author Comment

by:begonz
ID: 8156761
Thanks for this I will give it a shot.  

I guess in my in my main unit I can read the file at start up and populate global variables that can be used in claculation later.

How would I read in from the file and save to a variable?  This is quite a major part of what I'm trying to do.

What you have advised looks good, I will give it a try tonight and see how I go.

Thanks.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Expert Comment

by:RocketJockey
ID: 8156952
The following is the procedure that is run when your applicaiton starts up. If you want the values read from the INI file put into variables, simply replace the edit boxes with your variables.

If you want to add any new Sections or Values to the INI File use the following syntax:

TIniFile.WriteString('The Section Name', 'The Value's Name', 'The value');

procedure TForm2.FormCreate(Sender: TObject);
Var
 MyIniFile: TIniFile;
 strTaxRate, strICRate, strMLRate : String;
Begin
 //create the INI file
 MyIniFile := TIniFile.Create('C:\temp\ShareUser.ini');
 
 //set the TaxRate editbox
 strTaxRate := MyIniFile.ReadString('TaxPrefs', 'TaxRate', 'Default TaxRate');
 //set the ImputationCreditRate editbox
 strICRate := MyIniFile.ReadString('TaxPrefs', 'ICRate', 'Default ICRate');
 //set the MarginLendingRate editbox
 strMLRate  := MyIniFile.ReadString('TaxPrefs', 'MLRate', 'Default MLRate');
 
 MyIniFile.Free;
End;
0
 
LVL 3

Expert Comment

by:LukA_YJK
ID: 8157306
IniFile is good choice. Maybe you can do it simpler:
const
  INI_FILE = 'C:\temp\ShareUser.ini';
TForm2 = class(TForm)
...
private
...
  IniFile: TextFile;
  TaxRate: double;
  ICRate: double;
  MLRate: double;
end;

//Load all values every time you start
procedure TForm2.FormCreate(Sender: TObject);
Begin
  AssignFile(IniFile,INI_FILE);
  try
    Reset(IniFile);
    Readln(IniFile, TaxRate);
    Readln(IniFile, ICRate);
    Readln(IniFile, MLRate);
    CloseFile(IniFile);
  except
    TaxRate := 1.0;
    ICRate := 1.0;
    MLRate := 1.0;
  end;
End;

//Save all values every time you quit
procedure TForm2.FormClose(...);
Begin
  try
    Rewrite(IniFile);
    Writeln(IniFile, TaxRate);
    Writeln(IniFile, ICRate);
    Writeln(IniFile, MLRate);
    CloseFile(IniFile);
  except
  end;
End;
0
 

Author Comment

by:begonz
ID: 8163139
Awesome stuff RocketJockey, I have got it working.  Once thing though, how can I use the variables in other calcs?  How can I make them global?

Thanks again.
0
 

Author Comment

by:begonz
ID: 8163144
In addition to my last comment I guess I can just read the INI file for each calculation needed on each different form.  Is that an efficiant way of doing it?
0
 

Author Comment

by:begonz
ID: 8163168
Great help and does what I want.  Not sure if when I close the question you are still able to post comments so if you can answer my last email me at boss2000_nz@yahoo.co.nz

0
 

Expert Comment

by:RocketJockey
ID: 8163237
Nah, no need to keep reading the INI file. It is more effecient to define global variables. Simply place your variable declarations in the "public" section at the top of your main pas file.

To reference those Globals from other forms, just include the main form in the other form's Uses definitions.

public
{ Public declarations }
//Your Global vars
TAXRATE: double;
ICRATE: double;
MLRATE: double;

0
 

Author Comment

by:begonz
ID: 8163591
hmmmmm...

I have done as suggested and created 3 variables of Real type in the public section of my main pas file.

I have included the main pas file in the Uses section of the other calc but keep getting the message saying that 'Undeclared identifier: 'TAXRATE''

I am only trying to write to tthat Variable.

Any suggestions?
0
 

Author Comment

by:begonz
ID: 8163722
hmmmmm...

I have done as suggested and created 3 variables of Real type in the public section of my main pas file.

I have included the main pas file in the Uses section of the other calc but keep getting the message saying that 'Undeclared identifier: 'TAXRATE''

I am only trying to write to tthat Variable.

Any suggestions?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question