Do standard windows .ini files allow comments?

Viewed 154792

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.

6 Answers

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

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!

Related