Jump to content

IniWriteSection produces extra Ini File entry


Recommended Posts

I've been writing and reading to an ini file section. It works fine but the last value seems to add an extra entry on every write. The pertinent code is:

Dim $aSettings[7][2]

$aSettings[0][0] = "AspectRatio" ;key
$aSettings[0][1] = $FinalAspectRatio ;value
$aSettings[1][0] = "Time" ;key
$aSettings[1][1] = $ImageTime ;value
$aSettings[2][0] = "Folder" ;key
$aSettings[2][1] = $SelectedFolder ;value
$aSettings[3][0] = "DefFolder" ;key
$aSettings[3][1] = $DefaultFolder ;value
$aSettings[4][0] = "IsTitle" ;key
$aSettings[4][1] = $Title ;value
$aSettings[5][0] = "Counter" ;key
$aSettings[5][1] = $iStart ;value
$aSettings[6][0] = "Random" ;key
$aSettings[6][1] = $Randomness ;value

IniWriteSection($VidshowIni, "Settings", $aSettings, 0) ; Write to Ini

The ini looks like this after a few writes:

[settings]

AspectRatio=1.77777777777778

Time=3

Folder=D:\Download\\Ghost Whisperer

DefFolder=D:\Scrnsvr

IsTitle=2

Counter=20

Random=1

Random=1

Random=1

Random=1

The Random value is correct but each time an extra value is added to the bottom. Reading back the file works fine.

Also, if I start with the index = 1, I find that I get a "=" at the first position.

Link to comment
Share on other sites

Don't see the symptoms. Can you modify this demo to reproduce your symptoms?

Dim $VidshowIni = @ScriptDir & "\Test1.ini"
Dim $aSettings[7][2]
Dim $FinalAspectRatio = 1.77777777777778, $ImageTime = 3
Dim $SelectedFolder = "D:\Download\\Ghost Whisperer", $DefaultFolder = "D:\Scrnsvr"
Dim $Title = 2, $iStart = 20, $Randomness = 1

For $n = 1 To 100
    $aSettings[0][0] = "AspectRatio" ;key
    $aSettings[0][1] = $FinalAspectRatio ;value
    $aSettings[1][0] = "Time" ;key
    $aSettings[1][1] = $ImageTime ;value
    $aSettings[2][0] = "Folder" ;key
    $aSettings[2][1] = $SelectedFolder ;value
    $aSettings[3][0] = "DefFolder" ;key
    $aSettings[3][1] = $DefaultFolder ;value
    $aSettings[4][0] = "IsTitle" ;key
    $aSettings[4][1] = $Title ;value
    $aSettings[5][0] = "Counter" ;key
    $aSettings[5][1] = $iStart ;value
    $aSettings[6][0] = "Random" ;key
    $aSettings[6][1] = $Randomness ;value

    IniWriteSection($VidshowIni, "Settings", $aSettings, 0) ; Write to Ini
Next

Run('notepad.exe "' & $VidshowIni & '"')

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks for the suggestion. I tried the posted code and it worked fine with repeated ini writes. I tried the dimensioning in my script code and it made no difference. I tried the "n" times iniwrite too and it only added one extra Random=1 value.

I have a couple of other writes in the script to individual keys (by key name) but not to the one that is repeating. I disabled those just in case. If I got the repeating effect when writing to a single key, one would conclude that the existing key name is not being seen and so the iniwrite adds a new line. With the inisectionwrite, the key names are irrelevant - it should simply write the section key values in order of the array. The "n" times iniwrite seems to back that up.

Ongoing head scratching.

Link to comment
Share on other sites

You avoided the relevant question: Can you modify my demo, or post a running demo of your own, showing the symptoms?

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Sorry about that, I read it too quickly and modified my script, not yours.

I have found something interesting. I disabled the inireadsection at the start of my script and it seemed to cure the problem. I then added the code to yours and seem to have got the problem there too.

Dim $VidshowIni = @ScriptDir & "\Test1.ini"
Dim $aSettings[7][2]
Dim $FinalAspectRatio = 1.77777777777778, $ImageTime = 3
Dim $SelectedFolder = "D:\Download\Ghost Whisperer", $DefaultFolder = "D:\Scrnsvr"
Dim $Title = 2, $iStart = 20, $Randomness = 1


$VidshowIni = @ScriptDir & "\Test1.ini"
$aSettings = IniReadSection($VidshowIni, "Settings") ; read ini
$FinalAspectRatio = $aSettings[1][1]
$ImageTime = $aSettings[2][1]
$SelectedFolder = $aSettings[3][1]
$DefaultFolder = $aSettings[4][1]
$Title = $aSettings[5][1]
$iStart = $aSettings[6][1]
$Randomness = $aSettings[7][1]

For $n = 1 To 100
    $aSettings[0][0] = "AspectRatio" ;key
    $aSettings[0][1] = $FinalAspectRatio ;value
    $aSettings[1][0] = "Time" ;key
    $aSettings[1][1] = $ImageTime ;value
    $aSettings[2][0] = "Folder" ;key
    $aSettings[2][1] = $SelectedFolder ;value
    $aSettings[3][0] = "DefFolder" ;key
    $aSettings[3][1] = $DefaultFolder ;value
    $aSettings[4][0] = "IsTitle" ;key
    $aSettings[4][1] = $Title ;value
    $aSettings[5][0] = "Counter" ;key
    $aSettings[5][1] = $iStart ;value
    $aSettings[6][0] = "Random" ;key
    $aSettings[6][1] = $Randomness ;value

    IniWriteSection($VidshowIni, "Settings", $aSettings, 0) ; Write to Ini
Next

Run('notepad.exe "' & $VidshowIni & '"')

I have no idea why the read should affect the write. Even if the read values are incorrect (say displaced by one array position) it would not add the extra key entry on iniwrite?

Link to comment
Share on other sites

So you are taking the array from IniReadSection and modifying it? IniReadSection returns the size of the array in the [0][0] element. Therefore when you modify the values starting with 0 you don't update the last element of the array. Try this:

#include <Array.au3>

Dim $VidshowIni = @ScriptDir & "\Test1.ini"
Dim $aSettings[7][2]
Dim $FinalAspectRatio = 1.77777777777778, $ImageTime = 3
Dim $SelectedFolder = "D:\Download\Ghost Whisperer", $DefaultFolder = "D:\Scrnsvr"
Dim $Title = 2, $iStart = 20, $Randomness = 1


$VidshowIni = @ScriptDir & "\Test1.ini"
$aSettings = IniReadSection($VidshowIni, "Settings") ; read ini
_ArrayDisplay($aSettings)
$FinalAspectRatio = $aSettings[1][1]
$ImageTime = $aSettings[2][1]
$SelectedFolder = $aSettings[3][1]
$DefaultFolder = $aSettings[4][1]
$Title = $aSettings[5][1]
$iStart = $aSettings[6][1]
$Randomness = $aSettings[7][1]


$aSettings[0][0] = "AspectRatio" ;key
$aSettings[0][1] = $FinalAspectRatio ;value
$aSettings[1][0] = "Time" ;key
$aSettings[1][1] = $ImageTime ;value
$aSettings[2][0] = "Folder" ;key
$aSettings[2][1] = $SelectedFolder ;value
$aSettings[3][0] = "DefFolder" ;key
$aSettings[3][1] = $DefaultFolder ;value
$aSettings[4][0] = "IsTitle" ;key
$aSettings[4][1] = $Title ;value
$aSettings[5][0] = "Counter" ;key
$aSettings[5][1] = $iStart ;value
$aSettings[6][0] = "Random" ;key
$aSettings[6][1] = $Randomness ;value
_ArrayDisplay($aSettings)
IniWriteSection($VidshowIni, "Settings", $aSettings, 0) ; Write to Ini

You will notice that in the second _ArrayDisplay Random will be listed as the last two elements. This is why you are getting duplicates. Shift all of the items by one and everything will work correctly. In general, arrays should not be handled in this manor because you could end up getting an out of bounds error if IniReadSection does not return an array large enough to store the data you are setting. The better way to do this is to create the array yourself instead of reading it from the IniReadSection.

Link to comment
Share on other sites

You are reading the values into an array, shifting all the elements up one place (i.e. [1] to [0], and [7] to [6]) then rewriting the entire array back to the file. If you view the array before writing it to the ini file with _ArrayDisplay(), you'll see the extra element on the end.

It's writing exactly what you told it to.

If you only want elements [0] thru [6] then ReDim the array to trim off the extra element(s):

$aSettings[0][0] = "AspectRatio" ;key
$aSettings[0][1] = $FinalAspectRatio ;value

; ...

$aSettings[6][0] = "Random" ;key
$aSettings[6][1] = $Randomness ;value
ReDim $aSettings[7][2]
IniWriteSection($VidshowIni, "Settings", $aSettings, 0) ; Write to Ini

You could also move the data to different array of the correct size to write.

:idea:

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks for the responses. I was looking at the read as a means of extracting data. I had not looked at it from the point of view of creating the array. In the past I've read individual keys.

Normally the data for the script is set manually in a dialog and then written to the ini file. The read back is only for resuming at the point the previous use of the script was stopped if so desired.

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...