Do standard windows .ini files allow comments?

WindowsCommentsIni

Windows Problem Overview


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.

Windows Solutions


Solution 1 - Windows

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.

Solution 2 - Windows

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.

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.

Solution 3 - Windows

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.

Solution 4 - Windows

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

Solution 5 - Windows

Yes. Have a look at Wikipedia and Cloanto Implementation of INI File Format (see bottom of page).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoddyView Question on Stackoverflow
Solution 1 - WindowsIan BoydView Answer on Stackoverflow
Solution 2 - WindowsRoguePlanetoidView Answer on Stackoverflow
Solution 3 - WindowsAntoine MeltzheimView Answer on Stackoverflow
Solution 4 - WindowsThomas WellerView Answer on Stackoverflow
Solution 5 - WindowsKoekieboxView Answer on Stackoverflow