Solved

Getting blank line between sections in IniFiles

Posted on 2002-04-04
8
1,250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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