Recent

Author Topic: Reading an ini File(Solved)  (Read 7202 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Reading an ini File(Solved)
« on: January 26, 2018, 03:29:56 am »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms,
  9.   IniFiles, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Edit1: TEdit;
  18.     Edit2: TEdit;
  19.     Edit3: TEdit;
  20.     Edit4: TEdit;
  21.     Edit5: TEdit;
  22.     Label1: TLabel;
  23.     Label2: TLabel;
  24.     Label3: TLabel;
  25.     Label4: TLabel;
  26.     Label5: TLabel;
  27.     procedure Button1Click(Sender: TObject);
  28.     procedure FormCreate(Sender: TObject);
  29.     function iniRead(s : string): string;
  30.   private
  31.  
  32.   public
  33.  
  34.   end;
  35.  
  36. const
  37.   C_DB_SECTION = 'DB-INFO';
  38.  
  39. var
  40.   Form1: TForm1;
  41.  
  42.   INI                  : TINIFile;
  43.   Author               : String;
  44.   Pass                 : String;
  45.   DBFile               : String;
  46.   MaxAttempts          : integer;
  47.   PassEnter            : String;
  48.   TryCount             : integer;
  49.   iniPath              : String;
  50.  
  51. implementation
  52.  
  53. {$R *.lfm}
  54.  
  55. { TForm1 }
  56.  
  57. procedure TForm1.Button1Click(Sender: TObject);
  58. begin
  59.   Close;
  60. end;
  61.  
  62. procedure TForm1.FormCreate(Sender: TObject);
  63. Var
  64.    callResult  : String;
  65. begin
  66.   iniPath    := Application.Location;
  67.   callResult := iniRead(iniPath);
  68.   Edit1.Text := iniPath;
  69.  
  70. end;
  71.  
  72. procedure Readini(s : String);
  73. Begin
  74.  
  75. end;
  76.  
  77. function TForm1.iniRead(s : string): string;
  78. var
  79.   str: string;
  80.  
  81. begin
  82.  
  83.      INI := TINIFile.Create('DB,ini');
  84.  
  85.  // Put reading the INI file inside a try/finally block to prevent
  86.  // memory leaks
  87.       try
  88.         // Demonstrates reading values from the INI file.
  89.         Author       := INI.ReadString(C_DB_SECTION,'Author','');
  90.         Pass         := INI.ReadString(C_DB_SECTION,'Pass','');
  91.         DBFile       := INI.ReadString(C_DB_SECTION,'DBFile','');
  92.         MaxAttempts  := INI.ReadInteger(C_DB_SECTION,'MaxAttempts',1);
  93.  
  94.        // Show Results in Edit boxes on the screen
  95.  
  96.        Edit2.Text :=   Author;
  97.        Edit3.Text :=   Pass;
  98.        Edit4.Text :=   DBFile;
  99. //       Edit5.Text :=   MaxAttempts;
  100.  
  101.       finally
  102.      INI.Free;
  103.  
  104.       end;
  105.      str    := 'Process Worked';
  106.      Result := str;
  107.  
  108. end;
  109.  
  110. end.
  111.  




I copied the code from the org and adapted it to a GUI with edit boxes showing the results.
Nothing shows up except for Edit1. At line 66. I was just testing something and it has nothing to do with reading ini files.


http://wiki.freepascal.org/Using_INI_Files

 DB.ini in the dir where this executes as instructed in the Example.


[DB-INFO]
 Author=Adam
 Pass=secret
 MaxAttempts=5
 DBFile=C:\Money.dat
« Last Edit: January 26, 2018, 04:27:00 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Reading an ini File
« Reply #1 on: January 26, 2018, 03:53:54 am »
You have your file name wrong..

 Tinifile.Create('db.ini');

   You most likely are used to using a "," as a period in your local.

   need to watch out for that  ;)
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Reading an ini File
« Reply #2 on: January 26, 2018, 04:16:18 am »
You have your file name wrong..

 Tinifile.Create('db.ini');

   You most likely are used to using a "," as a period in your local.

   need to watch out for that  ;)

I'm having a real problem with my eyes right now amd it's difficult seeing thing on the screen.
Using a 55 inch 1080 LCD and still having difficulty.

I made the change and it worked perfectly.
Thank you very much.
 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018