Solved

Getting blank line between sections in IniFiles

Posted on 2002-04-04
8
1,186 Views
Last Modified: 2012-06-22
I have the feeling I'm overlooking something very obvious, but here it is:

using the following snippet of code:

uses
  IniFiles;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  sFile: string;
  fIni: TIniFile;
begin
  fIni := TIniFile.Create('c:\MyFile.txt');
  fIni.WriteString('Sect1', 'Key', '1');
  fIni.WriteString('Sect2', 'Key', '1');
end;

I get the following result in MyFile.txt:

[Sect1]
Key=1
[Sect2]
Key=1

while I really would like:

[Sect1]
Key=1

[Sect2]
Key=1

i.e: a blank line between the sections. This makes the file somewhat easier to read.

This can't be too difiicult, or is it? Thanks already for any advice,

Jm
0
Comment
Question by:jmvalk
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Accepted Solution

by:
SteveWaite earned 50 total points
ID: 6919574
const
  Crlf = #13#10;

fIni.WriteString('Sect1', 'Key', '1' + Crlf);
fIni.WriteString('Sect2', 'Key', '1' + Crlf);
0
 
LVL 17

Expert Comment

by:inthe
ID: 6919594
hi,
your doing nothing wrong ,it the way they are wrote ,i think its easier to use a stringlist etc and savetofile than adding blank lines in ini's :)
Tinifile is just a wrapper around WritePrivateProfileString()
0
 
LVL 17

Expert Comment

by:inthe
ID: 6919600
steve where did you find that '1'+ out !
i tried many combinations using #13#10 and couldnt get it working ,well done :)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:jmvalk
ID: 6919620
SteveWaite,

That was quick, thanks. Unfortunately, it isn't exactly what I'm looking for: Bacause if I use this setup for every write action, I'd get a blank line after every text line:

procedure TForm1.Button1Click(Sender: TObject);
const
 Crlf = #13#10;
var
  sFile: string;
  fIni: TIniFile;
begin
  fIni := TIniFile.Create('c:\jm.txt');
  fIni.WriteString('Sect1', 'Key', '1' + Crlf);
  fIni.WriteString('Sect2', 'Key', '1'+ Crlf);
  fIni.WriteString('Sect2', 'Key2', '2'+ Crlf);
end;

gives:
[Sect1]
Key=1

[Sect2]
Key=1

Key2=2

where it should be:
[Sect1]
Key=1

[Sect2]
Key=1
Key2=2

On the other hand, this works fine:
begin
  fIni := TIniFile.Create('c:\jm.txt');
  fIni.WriteString('Sect1', 'Key', '1' + Crlf);
  fIni.WriteString('Sect2', 'Key', '1'+ Crlf);
  fIni.WriteString('Sect1', 'Key2', '2');
end;

but that's because sect1 isn't the last section.

So, a work-around would be to make sure a dummy section exists as the last section at all times, and that new sections are created before it, using your Crlf-method. Strings to existing sections should then be written without the Crlf. For now, that's a bit too cumbersome, so I'll give this question a bit more time.

Thanks again,

Jm

0
 

Author Comment

by:jmvalk
ID: 6919632
SteveWaite,

That was quick, thanks. Unfortunately, it isn't exactly what I'm looking for: Bacause if I use this setup for every write action, I'd get a blank line after every text line:

procedure TForm1.Button1Click(Sender: TObject);
const
 Crlf = #13#10;
var
  sFile: string;
  fIni: TIniFile;
begin
  fIni := TIniFile.Create('c:\jm.txt');
  fIni.WriteString('Sect1', 'Key', '1' + Crlf);
  fIni.WriteString('Sect2', 'Key', '1'+ Crlf);
  fIni.WriteString('Sect2', 'Key2', '2'+ Crlf);
end;

gives:
[Sect1]
Key=1

[Sect2]
Key=1

Key2=2

where it should be:
[Sect1]
Key=1

[Sect2]
Key=1
Key2=2

On the other hand, this works fine:
begin
  fIni := TIniFile.Create('c:\jm.txt');
  fIni.WriteString('Sect1', 'Key', '1' + Crlf);
  fIni.WriteString('Sect2', 'Key', '1'+ Crlf);
  fIni.WriteString('Sect1', 'Key2', '2');
end;

but that's because sect1 isn't the last section.

So, a work-around would be to make sure a dummy section exists as the last section at all times, and that new sections are created before it, using your Crlf-method. Strings to existing sections should then be written without the Crlf. For now, that's a bit too cumbersome, so I'll give this question a bit more time.

Thanks again,

Jm

0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6919679
It only works with .ini files because Crlf denotes the end of a line, not the last bit of your string.

Also I didn't expect you to put it in every line :)

Regards,
Steve
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 6920207
This is what I use:


procedure BeautifyIni(const FName: string);
var
  SList, NewList: TStringList;
  i: Integer;
begin
  SList := TStringList.Create;
  NewList := TStringList.Create;
  try
    try
      SList.LoadFromFile(FName);
      if SList.Count > 1 then
      begin
        for i := 0 to SList.Count - 1 do
        begin
          if (Length(SList[i]) > 1) and (SList[i][1] = '[') and (i > 0)
            and (Trim(SList[i - 1]) <> '') then
            NewList.Add('');
          NewList.Add(SList[i]);
        end;
        NewList.SaveToFile(FName);
      end;
    except
    end;
  finally
    SList.Free;
  end;
end;
0
 

Author Comment

by:jmvalk
ID: 6922087
DragonSlayer,

your solution works, but it would mean that after every write action I will have to tell my program to loop completely through the file.

Apparently, there is no simple one-liner or property to tell TIniFile to keep an empty line between sections, so some programming has to be done. SteveWaite's suggestion lead me to the following:

Given a command to write info to section X, first check whether the section exists. If so, simply write the info. If not check whether any sections exist, if not, simply write the info.
If, however, the section does not yet exist, but others do, read the last key/value pair from the last section and write the info back to the file, but with an added CR/LF.
After that, simply write the info.

I've tested it and it works nicely, with only a limited amount of code.

Like I said, it was SteveWaite's suggestion that got me going, I will award the points to him. Thanks are for all of you, though.

Jm
0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: Connect to running MS Outlook 4 90
When i run adoquery my application freezes 26 166
JAudiorecorder record freezing the app 29 67
DBCtrlGrid, Delphi, Scroll 7 15
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

822 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