?
Solved

Getting blank line between sections in IniFiles

Posted on 2002-04-04
8
Medium Priority
?
1,284 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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