Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting blank line between sections in IniFiles

Posted on 2002-04-04
8
Medium Priority
?
1,308 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 100 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Industry Leaders: 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!

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…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

636 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