Lately I've been messing quite a bit with INI files with C++ and I want to share what I've learnt so far,
I've written many programs that use INI files, for example I have this one program which creates a window using the winAPI and then as soon as you close it, it saves the window width and height into an INI file. After they run the program again if an INI file exists, it reads the INI file and sets the width and height to the value read from the INI file. Allowing them to have the same sized window as they had when they last used it.
Now that was a basic use of an INI file and there are other things you can do, but understanding the concept comes first.
An INI file is a file which configures a program set to read from it. They have a special format, a bit like a programming language but a lot easier to learn as it requires less than 5 minutes to understand. An INI file is a simple text file and is based on Windows, it's also used on other platforms but on Windows it is now deprecated, instead Data is stored into the
registry. An INI file commonly exists with the .ini extension but you may find it in ".cfg", ".config" and even the rare ".txt".
Each INI file contains a section, a section is like the parent of the variables that are later going to be read. Everything starts with a section and you can have as many sections as you want. This may be a bad analogy but think of a section as a book's chapter name, the chapter contains much text, being the story - or - imagine a box, all the boxes are the same size and too differentiate between them you need to name them, and possible write a label which sticks on to them, that is a bit like the section in this case.
So a section would be shown like this:
[Section1]
[Section2]
[MyOwnNamedSection]
|
Now, you want to give each "box" (section) something to put inside of it? How about a variable index with a toggle value of 1 and 0 with the parameter name toggleVal in a section named ToggleValue:
[ToggleValue]
toggleVal=1
|
Note: All sections can have as many variable indexes/parameters as you want, and an INI file can have as many sections as you want.
Now as all good programmers should comment their work, the same concept applies here just with a different trigger, in DM and other selected languages, the comment trigger would be "//" (2 forward slashes), but with INI files it's ";" (semi-colon). So for those who haven't really worked with many programming languages.
Here is the example shown earlier with comments:
[ToggleValue]
toggleVal=1 ;This value can be either 1 and 0, 1 being true and 0 being false.
|
Now here is a larger example:
;Last updated June 13th 1994
[WindowConfig]
width=456 ;Width of window
height=345 ;Height of window
bgcolour=#4523CF ;Window background colour
fgcolour=#FFFFFF ;Window forground colour
title="INI with Windows" ;Title of my window
allow_resize=1 ;Toggle 1 and 0 to allow/disallow window resizing
allow_minimise=1 ;Toggle 1 and 0 to allow/disallow window minimising
allow_maximise=0 ;Toggle 1 and 0 to allow/disallow window maximising
[OtherParemeters]
call_it=3453543
[Messages]
on_enter="Welcome to INI with Windows"
on_close="Bye, I hope you had a great time"
|
As you may notice I've assigned text as well as numbers to some parameters, which is perfectly fine, I've wrapped the text with (")'s, that's because most API's that read the parameters need the (")'s to help them read the variable, it's usually used to differentiate numbers and integers.
I hope you've understood the basics, read more about INI files
here.
To read INI files use
this library named "INI reader" written by
Audeuro.
Haywire
Posted by Haywire on Saturday, April 25, 2009 08:01AM
- 0 comments
(link)
/
Members say:
yea +1,
nay -2