Jump to content

Question: proper way to write to file?


Recommended Posts

Hi,

I have a question about what i would take as proper practices in writing to a file like a settings.ini.
would it even matter? diskwrites vs diskreads? speed? other reasons? other methods? Does Example1 even actually write when the value is thesame?

Example1 (IniRead and IniWrite are incorrect, just an example):

$Something_Var01 = IniRead("The settings file and the corresponding value")
$Something_Var02 = IniRead("The settings file and the corresponding value")
$Something_Var03 = IniRead("The settings file and the corresponding value")
$Something_Var04 = IniRead("The settings file and the corresponding value")
$Something_Var05 = IniRead("The settings file and the corresponding value")
;...etc

;..do app stuff..

Func _Save_Ini()
    ;Even if $Var is unchanged from IniRead
    IniWrite("The settings file and the corresponding",$Something_Var01)
    IniWrite("The settings file and the corresponding",$Something_Var02)
    IniWrite("The settings file and the corresponding",$Something_Var03)
    IniWrite("The settings file and the corresponding",$Something_Var04)
    IniWrite("The settings file and the corresponding",$Something_Var05)
    ;...etc
    Exit
EndFunc


Example2 (IniRead and IniWrite are incorrect, just an example):

$Something_Var01 = IniRead("The settings file and the corresponding value")
$Something_Var02 = IniRead("The settings file and the corresponding value")
$Something_Var03 = IniRead("The settings file and the corresponding value")
$Something_Var04 = IniRead("The settings file and the corresponding value")
$Something_Var05 = IniRead("The settings file and the corresponding value")
;...etc

;..do app stuff..

Func _Save_Ini()
    ;Only if $Var is different from IniRead
    If $Something_Var01 <> IniRead("The settings file and the corresponding value") Then IniWrite("The settings file and the corresponding",$Something_Var01)
    If $Something_Var01 <> IniRead("The settings file and the corresponding value") Then IniWrite("The settings file and the corresponding",$Something_Var02)
    If $Something_Var01 <> IniRead("The settings file and the corresponding value") Then IniWrite("The settings file and the corresponding",$Something_Var03)
    If $Something_Var01 <> IniRead("The settings file and the corresponding value") Then IniWrite("The settings file and the corresponding",$Something_Var04)
    If $Something_Var01 <> IniRead("The settings file and the corresponding value") Then IniWrite("The settings file and the corresponding",$Something_Var05)
    ;...etc
    Exit
EndFunc

 

Edited by Aapjuh
Link to comment
Share on other sites

Since you're only overwriting the values you can just use IniWrite without the IniRead or use the function more efficiently, for example:

_SaveIni("Key1", "ExpectedValue1")
_SaveIni("Key2", "ExpectedValue2")
_SaveIni("Key3", "ExpectedValue3")

Func _SaveIni($_sKey, $_sValue)
    Local $sFilePath = @ScriptDir & "\FilePath.ini"
    Local $sSection = "Section"
    Local $sIniRead = IniRead($sFilePath, $sSection, $_sKey, "")
    If $sIniRead = $_sValue Then Return
    IniWrite($sFilePath, $sSection, $_sKey, $_sValue)
EndFunc

 

Link to comment
Share on other sites

2 hours ago, Subz said:

Since you're only overwriting the values you can just use IniWrite without the IniRead or use the function more efficiently, for example:

@SubzThat's a very elegant solution.

Saving for future use. Thank you.

taurus905

"Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs

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