IniRead requires blank line

Get help with using AutoHotkey and its commands and hotkeys
Alfonso S Michaelis
Posts: 3
Joined: 11 Jun 2018, 18:49

IniRead requires blank line

27 Oct 2018, 11:26

I appear to have run into a problem where IniRead will not read a section if the section header is the first line of the ini file.

In my ahk file I have:

Code: Select all

configPath := "config.ini"

IniRead, exitScript, %configPath%, Bindings, exitScript, ERROR
IniRead, button, %configPath%, Bindings, button, ERROR
IniRead, loopCount, %configPath%, Settings, loopCount, ERROR

MsgBox, % "exitScript = " . exitScript . "`n"
    . "button = " . button . "`n"
    . "loopCount = " . loopCount
In my ini file I have:

Code: Select all

[Bindings]
exitScript=Esc
button=!+b

[Settings]
loopCount=5
And the msgBox produces the following output:

Code: Select all

exitScript = ERROR
button = ERROR
loopCount = 5

However, if I change my ini file to:

Code: Select all


[Bindings]
exitScript=Esc
button=!+b

[Settings]
loopCount=5
The output of the msgBox becomes:

Code: Select all

exitScript = Esc
button = !+b
loopCount = 5

This does not seem like something that should be happening, and I have used a similar setup in other scripts without encountering this problem. Does anyone know what might be happening?
aifritz
Posts: 260
Joined: 29 Jul 2018, 11:30
Location: Germany

Re: IniRead requires blank line

27 Oct 2018, 11:45

I can't understand that. Your script produces always an error.
Please try to define the full path for your ini-file. Then for me it is working.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: IniRead requires blank line

27 Oct 2018, 11:50

- Your ini file is probably a UTF-8 file with a BOM. The Windows API functions (e.g. GetPrivateProfileString / WritePrivateProfileString) are only designed to handle ANSI and UTF-16 files. However, there are workarounds to use UTF-8 files.

- If the ini file is a UTF-8 file, Windows views it as an ANSI file, and sees this:

Code: Select all

[MySection]
- A fix is to add a blank line:

Code: Select all


[MySection]
- Or a comment:

Code: Select all

;my comment
[MySection]
- You will not see the BOM () (bytes: EF BB BF) when editing the file.

- More info on UTF-8 and ini files:
UTF-8 ini files - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38511
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Alfonso S Michaelis
Posts: 3
Joined: 11 Jun 2018, 18:49

Re: IniRead requires blank line

27 Oct 2018, 12:03

aifritz wrote:
27 Oct 2018, 11:45
I can't understand that. Your script produces always an error.
Please try to define the full path for your ini-file. Then for me it is working.
I had tried with full path as well, same result.

jeeswg wrote:
27 Oct 2018, 11:50
- If the ini file is a UTF-8 file, Windows views it as an ANSI file, and sees this:

Code: Select all

[MySection]
- A fix is to add a blank line:

Code: Select all


[MySection]
- Or a comment:

Code: Select all

;my comment
[MySection]
- You will not see the BOM () when editing the file.
- More info on UTF-8 and ini files:
UTF-8 ini files - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=38511
Interesting. That would certainly explain the problem. Thanks.

After some more testing I found out why it was a new problem. Visual Studio Code does not add the BOM, but Windows Explorer does. So depending on how you create the file, the issue may or may not appear.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], jadams, LC99, mikeyww, songdg and 40 guests