Solved

Getting blank line between sections in IniFiles

Posted on 2002-04-04
8
1,171 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now