Re: outputting settings to a file..

Posted by webmaster Guido on October 22, 2001

In Reply to: outputting settings to a file.. posted by p12174 Doug on October 22, 2001

: when some one uses the front-end app, and they click save changes, it should save their choices out to a file. the file looks similar to an INI file, but not the same set up.

: example:

: ProjectSettings
: FirstChoice 1
: SecondChoice 0
: (where 1 is on and 0 is off)
: and so on..

: what i'm trying to do, is get the user(s) input from the various checkboxes, edit fields, etc.. and then have those choices saved in the config file.

Even if you don't want to use the standard INI-file format, I advise you to start by trying the following example and afterwards build on that code to develop your own.

Example: a form with two buttons, one for writing data to the INI-file and one for reading back the data. Don't forget to add "INIFILES" to the uses-clause on top of the unit, otherwise Delphi will not compile it!

(Tip: instead of using the two buttons, you can also read and write the settings automatically if you use the form's OnActivate and OnClose events)

procedure TForm1.ButtonWriteClick(Sender: TObject);
var
  IniFile: TIniFile;
begin
  IniFile := TIniFile.Create('C:\Test\Settings.ini');
  with IniFile do begin
    WriteBool('ProjectSettings', 'FirstChoice', CheckBox1.Checked);
    WriteBool('ProjectSettings', 'SecondChoice', CheckBox2.Checked);
    // and so on for other settings...
    Free;
  end;
end;

procedure TForm1.ButtonReadClick(Sender: TObject);
var
  IniFile: TIniFile;
begin
  IniFile := TIniFile.Create('C:\Test\Settings.ini');
  with IniFile do begin
    CheckBox1.Checked := ReadBool('ProjectSettings', 'FirstChoice', False);
    CheckBox2.Checked := ReadBool('ProjectSettings', 'SecondChoice', False);
    // and so on for other settings...
    Free;
  end;
end;

TInifile features:
- You don't have to worry about the order of the lines.
- All the conversions of values to text and vice versa are done automatically. Just use WriteBool and ReadBool, WriteInteger and ReadInteger, and so on (see Delphi's Help).
- "Read" doesn't give an error if the ini-file does not exist: then it simply takes the default value that you supply as the last parameter of the "read" (the value False in the example above).
- You don't have to save your data in a file with an INI-extension, it works for any file extension. So you might call the file CONFIG.DAT or PROGRAM.CFG or whatever.

For writing data in our own format, such as the format that you suggested, we firstly have to write a couple of conversion functions:

function BoolToIniText(Name: string; Value: Boolean): string;
var
  C: char;
begin
  if Value then C := '1' else C := '0';
  Result := Name + ' ' + C;
end;

function IniTextToBool(S: string; FileFound, DefaultValue: Boolean): Boolean;
var
  C: char;
begin
  if not FileFound then Result := DefaultValue
  else begin
    C := S[Length(S)]; // the last character
    Result := C = '1';
  end;
end;

Next, we can build on the INI-file example:

procedure TForm1.ButtonWriteClick(Sender: TObject);
var
  MyIni: TStringList;
begin
  MyIni := TStringList.Create;
  with MyIni do begin
    Add('ProjectSettings');
    Add(BoolToIniText('FirstChoice', CheckBox1.Checked));
    Add(BoolToIniText('SecondChoice', CheckBox2.Checked));
    // and so on for other settings...
    SaveToFile('C:\Test\Settings.ini');
    Free;
  end;
end;

Reading back the data is easy if it is done in the same sequence as you have written it. But it involves quite a bit more if you want to be able to read at random, like you can with TInifile!
Below is an example for reading back in the data, without looking at the name(s) of the section(s) or looking at the names of the values:

procedure TForm1.ButtonReadClick(Sender: TObject);
var
  MyIni: TStringList;
  Found: Boolean;
begin
  Found := FileExists('C:\Test\Settings.ini');
  MyIni := TStringList.Create;
  MyIni.LoadFromFile('C:\Test\Settings.ini');
  // skip the section header 'ProjectSettings'
  // so start with the SECOND line
  CheckBox1.Checked := IniTextToBool(MyIni[1], Found, False);
  CheckBox2.Checked := IniTextToBool(MyIni[2], Found, False);
  // and so on for other lines...
  MyIni.Free;
end;

Tip: for storing integer values, you can use the Delphi functions IntToStr() and StrToInt() to make the necessary conversions.


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]