Do standard Windows .ini files allow comments?
Asked Answered
H

6

128

Are comments allowed in Windows ini files? (...assuming you're using the GetPrivateProfileString api functions to read them...)

[Section]
Name=Value   ; comment
    
; full line comment

And, is there a proper spec of the .INI file format anywhere?

Thanks for the replies - However maybe I wasn't clear enough. It's only the format as read by Windows API Calls that I'm interested in. I know other implementations allow comments, but it's specifically the MS Windows spec and implementation that I need to know about.

Happiness answered 4/9, 2009 at 9:42 Comment(3)
The API returns the Value and the Comment, you just need to look for the SemiColon and read, or strip out this when encounter, using a regular expression or just look for the ;Jagir
@Jagir A regular expression, to find a ; character? Seriously!?Fortuneteller
@Fortuneteller Could use the regex to read the comment only so extract the element after the semi-colon, or as I mentioned in my comment or look for the ; giving the alternative to extract the comment itself or look for the presence of one I thought my comment added value, just seeing if a semi-colon was there was an incomplete commentJagir
H
138

Windows INI API support for:

  • Line comments: yes, using semi-colon ;
  • Trailing comments: No

The authoritative source is the Windows API function that reads values out of INI files

GetPrivateProfileString

Retrieves a string from the specified section in an initialization file.

The reason "full line comments" work is because the requested value does not exist. For example, when parsing the following ini file contents:

[Application]
UseLiveData=1
;coke=zero
pepsi=diet   ;gag
#stackoverflow=splotchy

Reading the values:

  • UseLiveData: 1
  • coke: not present
  • ;coke: not present
  • pepsi: diet ;gag
  • stackoverflow: not present
  • #stackoverflow: splotchy

Update: I used to think that the number sign (#) was a pseudo line-comment character. The reason using leading # works to hide stackoverflow is because the name stackoverflow no longer exists. And it turns out that semi-colon (;) is a line-comment.

But there is no support for trailing comments.

Hendrickson answered 23/10, 2013 at 18:55 Comment(4)
Very helpful examples, but I find the wording "The reason 'full line comments' work is because the requested value does not exist" hard to parse. I would clarify to say the Windows INI implementation requires the semicolon before the key/value pair (if present), otherwise it is considered part of the value.Barnstorm
@Happiness You're right. I was so focused on using # that i didn't pay attention to the ;. Fixed the answer to point out that semi-colon really is a line comment.Hendrickson
What is meant by "no longer exists?" Does that mean it exists at some point?Eudo
@GeneralGrievance It did exist—as stackoverflow—before the hypothetical developer changed it to #stackoverflow.Hendrickson
J
31

I have seen comments in INI files, so yes. Please refer to this Wikipedia article. I could not find an official specification, but that is the correct syntax for comments, as many game INI files had this as I remember.

From the article

Semicolons (;) at the beginning of the line indicate a comment. Comment lines are ignored.

It also mentions that # are allowed, but only in some formats. It's not clear on whether it's allowed on Windows.

Edit

The API returns the Value and the Comment (forgot to mention this in my reply), just construct and example INI file and call the API on this (with comments) and you can see how this is returned.

Jagir answered 4/9, 2009 at 9:45 Comment(3)
When using sources, it makes more sense to use the most authoritative source and then any supporting sources.Tricotine
Sure that's a good idea - never too late to point that out - even after seven yearsJagir
I added my comment about game INI processing to my otherwise single-character edit of this answer to avoid putting in a comment such as "This is only here to make the edit more than 6 characters and can be deleted" - otherwise it would be a comment here.Hogfish
E
12

USE A SEMI-COLON AT BEGINING OF LINE --->> ; <<---

Ex.

; last modified 1 April 2001 by John Doe
[owner]
name=John Doe
organization=Acme Widgets Inc.
Erlandson answered 6/4, 2018 at 12:47 Comment(0)
L
2

I like the analysis of @Ian Boyd, because it is based on the official GetPrivateProfileString() method of Microsoft.

In my attempts of writing a Microsoft compatible INI parser, I'm having a closer look at the said Microsoft API and for comments I found out:

  • you can have line comments using semicolon
  • the semicolon needn't be the first character of the line; it can be preceded by space, tab or vertical tab
  • you can have trailing "comments" after a section even without semicolon. It's probably not intended to be a comment, but the parser will ignore it.
  • values outside a section cannot be accessed (at least I did not find a way), effectively making them useless except for commenting purposes
  • certainly abuse, but the parser overflows at 65536 characters, so anything after that will not be part of the value either. I would not rely on this, since Microsoft could fix this in later versions of Windows. Also, it's not very useful as a comment when you don't see it.

Example:

this=cannot be accessed
[section]this=is ignored
;this=is a line comment
      ;this=is a comment preceded by spaces
key=value                                <... 65530 spaces ...>this=cannot be parsed
Larimor answered 21/12, 2021 at 9:11 Comment(2)
Knowing how the rules can be bent can be useful information - for the current implementation. As you say, though, future implementations could change the way the bends work - and there may be historical ones that did things differently, too ...Hogfish
Also note that values outside a section are ignored by the Windows API (which is what the OP was asking about) but are treated as "global variables" by some other INI parsers.Hogfish
L
0

Yes, it allows.

The way to comment is to use ; for a new line rather than just after the content you want to comment in the same line, which is allowable for other files where you want to comment.

Let me show you an example:

I use .ini file to pass some parameters for my training file when I use SUMO software. If I write like this:

width_layers = 400 ;the number of neurons per layer in the neural network.

I will get an error message which is

ValueError: invalid literal for int() with base 10: '400 ;the number of neurons per layer in the neural network.'

I have to create a line for that, which is

width_layers = 400
;the number of neurons per layer in the neural network.

Then, it will work. Hope it helps in detail!

Lalise answered 26/7, 2022 at 0:21 Comment(0)
S
-1

You can use ; and # to comment in .ini files as shown below. *You can see How to write a Comments in INI file?:

; commnet
# comment
Subtract answered 4/8, 2023 at 17:52 Comment(1)
The page linked does not recommend # as used here. Also, it doesn't say anything about Windows.Eudo

© 2022 - 2024 — McMap. All rights reserved.