Jump to content

IniWriteSection data as string: @LF or @CRLF ?


Go to solution Solved by BrewManNH,

Recommended Posts

from the help of IniWriteSection()

 

"If the data is a string, then each key=value pair must be delimited by @LF."

here @LF is used to separate key/value pairs.

 

from the example of IniRenameSection()

Local $sSection = "Title=AutoIt" & @CRLF & "Version=" & @AutoItVersion & @CRLF & "OS=" & @OSVersion

here @CRLF is used to separate key/value pairs.

 

so which is it? i tried and i get that @CRLF is needed, because @LF alone does not pass to next line. so either the help needs correction, or i'm doing something wrong...

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

  • Solution

You can technically use either @LF or @CRLF to end the key/value pairs, the only difference is that if you use @CRLF, as far as I can tell, is that you get an extra blank line in your ini file and it screws up the line endings.

 

I modified the example script from the help file for the IniWriteSection function to use a string instead of an array. I also used both @LF and @CRLF in creating the string.

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Create an INI section structure as an array. The zeroth element is how many items are in the array, in this case 3.
    Local $aSection = "3=''" & @LF & "Title = AutoIt" & @CRLF & "Version=" & @AutoItVersion & @CRLF & "OS=" & @OSVersion

    ; Write the array to the section labelled 'General'.
    IniWriteSection(@TempDir & "\Example.ini", "General", $aSection)

    ; Read the INI section labelled 'General'. This will return a 2 dimensional array.
    Local $aArray = IniReadSection(@TempDir & "\Example.ini", "General")

    ; Check if an error occurred.
    If Not @error Then
        ; Enumerate through the array displaying the keys and their respective values.
        For $i = 1 To $aArray[0][0]
            MsgBox($MB_SYSTEMMODAL, "", "Key: " & $aArray[$i][0] & @CRLF & "Value: " & $aArray[$i][1])
        Next
    EndIf

    ; Delete the INI file.
    FileDelete(@TempDir & "\Example.ini")
EndFunc   ;==>Example
The output to the message boxes are the same with either delimiter, but the result in the INI file is quite different.

 

[General]CRLF

3=''CRLF

Title = AutoIt CR

CRLF

Version=3.3.12.0 CR

CRLF

OS=WIN_7 CR

CRLF

As you can see by the above, if you use @CRLF, the line strips the LF and leaves the CR, and then adds a blank CRLF line in the ini file. If you use @LF, as in the line that starts with 3, you'll see it behaves normally.

So, don't use @CRLF in an ini write section or you'll end up with a non-conventional ini file. Saying this, that means that the example script for IniRenameSection needs to be rewritten to follow the help file.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

thank you BrewManNH for the detailed answer, that was very helpful!

Signature - my forum contributions:

Spoiler

UDF:

LFN - support for long file names (over 260 characters)

InputImpose - impose valid characters in an input control

TimeConvert - convert UTC to/from local time and/or reformat the string representation

AMF - accept multiple files from Windows Explorer context menu

DateDuration -  literal description of the difference between given dates

Apps:

Touch - set the "modified" timestamp of a file to current time

Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes

SPDiff - Single-Pane Text Diff

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...