Jump to content

How to optimally write 200 values in .ini file


AZJIO
 Share

Recommended Posts

1. If I write 200 values, the ini-file will be overwritten 200 times. This is not economical.

#include <GUIConstantsEx.au3>

; Свой реестр
$Ini = @ScriptDir & '\prog.ini'
$ifTrChMax = Number(IniRead($Ini, 'Setting', 'Max', '0'))

$hGui = GUICreate('My Program', 250, 260)
$iChMax = GUICtrlCreateCheckbox("Checkbox 1", 10, 10, 120, 20)
If $ifTrChMax Then GUICtrlSetState(-1, $GUI_CHECKED)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $iChMax
            $ifTrChMax = _ChReadToBool($iChMax)
        Case -3
            GUIDelete($hGui)
            _Exit()
    EndSwitch
WEnd

Func _ChReadToBool($iID)
    If BitAND(GUICtrlRead($iID), $GUI_CHECKED) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>_ChReadToBool

Func _Exit()
    IniWrite($Ini, "Setting", "Max", $ifTrChMax)
    Exit
EndFunc   ;==>_Exit

2. If duplicate 2 status and check the difference, the number of entries will depend on the modified flags.

#include <GUIConstantsEx.au3>

; Свой реестр
$Ini = @ScriptDir & '\prog.ini'
$ifTrChMax = 0
$iniMax = Number(IniRead($Ini, 'Setting', 'Max', '0'))
$ifTrChMax = $iniMax

$hGui = GUICreate('My Program', 250, 260)
$iChMax = GUICtrlCreateCheckbox("Checkbox 1", 10, 10, 120, 20)
If $ifTrChMax Then GUICtrlSetState(-1, $GUI_CHECKED)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $iChMax
            $ifTrChMax = _ChReadToBool($iChMax)
        Case -3
            GUIDelete($hGui)
            _Exit()
    EndSwitch
WEnd

Func _ChReadToBool($iID)
    If BitAND(GUICtrlRead($iID), $GUI_CHECKED) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>_ChReadToBool

Func _Exit()
    ; Если изменилось с момента чтения ini-файла, то записать новое значение
    If $ifTrChMax <> $iniMax Then IniWrite($Ini, "Setting", "Max", $ifTrChMax)
    Exit
EndFunc   ;==>_Exit

3. If before exiting the program to read the ini-file into an array and make the upgrade in memory, the overwritten will be executed one time. (>IniVirtual.au3)

#include <GUIConstantsEx.au3>
#include <IniVirtual.au3>

; Свой реестр
$Ini = @ScriptDir & '\prog.ini'
$ifTrChMax = Number(IniRead($Ini, 'Setting', 'Max', '0'))

$hGui = GUICreate('My Program', 250, 260)
$iChMax = GUICtrlCreateCheckbox("Checkbox 1", 10, 10, 120, 20)
If $ifTrChMax Then GUICtrlSetState(-1, $GUI_CHECKED)
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $iChMax
            $ifTrChMax = _ChReadToBool($iChMax)
        Case -3
            GUIDelete($hGui)
            _Exit()
    EndSwitch
WEnd

Func _ChReadToBool($iID)
    If BitAND(GUICtrlRead($iID), $GUI_CHECKED) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>_ChReadToBool

Func _Exit()
    Local $a_ini_Main2D, $hFile, $s_ini_Text
    $s_ini_Text = FileRead($Ini)
    $a_ini_Main2D = _IniVirtual_Initial($s_ini_Text)
    _IniVirtual_Write($a_ini_Main2D, 'Setting', 'Max', $ifTrChMax)
    $s_ini_Text = _IniVirtual_Save($a_ini_Main2D)
    $hFile = FileOpen($Ini, 2)
    FileWrite($hFile, $s_ini_Text)
    FileClose($hFile)
    Exit
EndFunc   ;==>_Exit

I chose the last option. There are other opinions?

I know that the registry is better, but I'm taking into account the transfer on a flash drive.

Edited by AZJIO
Link to comment
Share on other sites

He writes with one stream?

Maybe I should not bother.

(1000 launch) * (20-40 parameters) = (20 000 rewrite)

5 KB * 20 000 = 100 MB

Some programs can only be configured when you first start. Or 10 starts, one of them an rewrite in the ini-file.

I want to decide on the choice of algorithm, then not to remake.

Link to comment
Share on other sites

Why not manually create the ini in the form of an array or variable and then transfer that to a file directly? You can still use a normal iniread but at least the write would be a single process to transform the variable to saved file.

Link to comment
Share on other sites

I think AZJIO want to use that in an intensive concurrent read / write environment where many instances of that task will run on the same ini. (or not?.... in that form all tasks access the same key within the ini)
... in that case I also would take in consideration the use of SQLite as in >this jchd's example for intensive concurrent r/w acces to a database

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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