Recent

Author Topic: [Solved] TIniFile - ReadSection  (Read 14202 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
[Solved] TIniFile - ReadSection
« on: February 26, 2011, 12:55:35 pm »
Greetings,

I've been struggling with this too long.  What's wrong with this code?

procedure TForm1.mnuOpenFileClick(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(UTF8ToSys(ChangeFileExt(Application.ExeName,'.vrb')));
  if OpenDlg.Execute then begin
    FName:= OpenDlg.FileName;
    Ini.ReadSection('Items',cbxShem.Items);  // ---- This seems to do nothing
    if cbxShem.Items.Count>0 then
      cbxShem.ItemIndex:= 0;
  end;
  FreeAndNil(Ini);
end;

The "ReadSection" doesn't do what I am expecting.
« Last Edit: February 26, 2011, 03:57:20 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: TIniFile - ReadSection
« Reply #1 on: February 26, 2011, 01:06:09 pm »
This is the code that creates the IniFile:

procedure TForm1.mnuSaveVerbClick(Sender: TObject);
var
  I: Integer;
  Ini: TIniFile;
  Lst: TStringLIst;
  IniStr: String;
begin
  Lst:= TStringList.Create;
    for I:= 0 to Form1.ComponentCount-1 do begin
      if Form1.Components is TComboBox then
        with Form1.Components as TComboBox do
          Lst.Add(Text);
      if Form1.Components is TEdit then
        with Form1.Components as TEdit do
          Lst.Add(Text);
    end;
    IniStr:= Lst.CommaText;
  Ini := TIniFile.Create(UTF8ToSys(ChangeFileExt(Application.ExeName,'.vrb')));
     Ini.WriteString('Items',Lst[0],IniStr);
  FreeAndNil(Ini);
  FreeAndNil(Lst);
end;
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: TIniFile - ReadSection
« Reply #2 on: February 26, 2011, 01:24:30 pm »
Very strange.  I manually created a Section above the Section I am trying to read and now everything works.  Go figure.  I read in another post that the first Section was being ignored so I thought maybe that was it.
Lazarus Trunk / fpc 2.6.2 / Win32

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TIniFile - ReadSection
« Reply #3 on: February 26, 2011, 01:34:09 pm »
I don't know if I understand your code (and your goal) correctly but you use:

ReadSection and WriteString - which are not opposite function.

ReadSection reads all section names (and not their content).

WriteString is oppsite to ReadString.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: TIniFile - ReadSection
« Reply #4 on: February 26, 2011, 01:40:20 pm »
My goal is to fill a TComboBox.Items with all of the Section Keys, so ReadSection seems to be what I want.
Lazarus Trunk / fpc 2.6.2 / Win32

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TIniFile - ReadSection
« Reply #5 on: February 26, 2011, 01:46:54 pm »
 But you write everything to only one section with name 'Items'

Code: [Select]
Ini.WriteString('Items',Lst[0],IniStr);
so then ReadSection can find only one section IMO.

EDIT: Sorry, I wanted tell that you write only one Key to Section 'Items'

I use ini files and first section is not ignored.
« Last Edit: February 26, 2011, 01:56:49 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Lush

  • New Member
  • *
  • Posts: 15
Re: TIniFile - ReadSection
« Reply #6 on: February 26, 2011, 02:11:36 pm »
I read in another post that the first Section was being ignored so I thought maybe that was it.
In that other thread you mention, the first section was being ignored because of the file encoding. It was a UTF-8 (or 16) file that had a BOM at the beginning and thus messed up the first section title.

Encode your file as ANSI, or UTF-8 without BOM and try again.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: TIniFile - ReadSection
« Reply #7 on: February 26, 2011, 02:19:04 pm »
Since I use  "Ini := TIniFile.Create(UTF8ToSys(ChangeFileExt(Application.ExeName,'.vrb')));" to write and read the file, it should be UTF8 without BOM.  But I checked with NotePad++ and it is UTF8 without BOM as it should be.  Still, it seems to overlook the first section.  I'm still looking for the answer but if I find it, I'll post it for others.
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: TIniFile - ReadSection
« Reply #8 on: February 26, 2011, 03:36:27 pm »
Well, I don't have THE answer, but I solved the problem.  I closed down Lazarus and restarted it and everything seems to work fine.

I'm new to Lazarus so forgive me if I'm asking an obvious question.  I notice that on some of the posts, it says "[Solved]".  How do you do that?
Lazarus Trunk / fpc 2.6.2 / Win32

Lush

  • New Member
  • *
  • Posts: 15
Re: TIniFile - ReadSection
« Reply #9 on: February 26, 2011, 03:54:06 pm »
I notice that on some of the posts, it says "[Solved]".  How do you do that?
Just edit your first post (the "modify" link) and change your title :)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: TIniFile - ReadSection
« Reply #10 on: February 26, 2011, 03:56:56 pm »
Yes, of course  ::) [Solved]!  Thanks for putting up with a newbe.
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018