Click here to Skip to main content
15,890,123 members
Articles / Programming Languages / C++
Article

CIniFile

Rate me:
Please Sign up or sign in to vote.
4.39/5 (16 votes)
31 May 2005 453.4K   2.8K   86   151
A class that makes it easy to implement an INI settings file in your applications.

Sample Image - cinifile.gif

Introduction

In every program I have written, I end up using some sort of INI file to keep settings from one run to the next. Instead of implementing it separately in each program, I finally got around to writing this class, CIniFile. It is simple to set up and use.

After you create your CIniFile object, call the member function SetPath(CString newpath) to set the path/filename for the INI file to read from and write to.

To read the INI file data into the class, call ReadFile().

To retrieve data from the class, use GetValue or one of its overloads:

//returns value of keyname/valuename as CString
CString GetValue(CString keyname, CString valuename);

//returns value of keyname/valuename as int
int GetValueI(CString keyname, CString valuename);
 
//returns value of keyname/valuename as double
double GetValueF(CString keyname, CString valuename);

To set data values in the class, call SetValue or one of its overloads:

bool SetValue(CString key, CString valuename, CString value, bool create = 1); 
bool SetValueI(CString key, CString valuename, int value, bool create = 1);
bool SetValueF(CString key, CString valuename, double value, bool create = 1);

Use the optional parameter create as false if you do not want it to create the key/value if it doesn't already exist.

SetValue returns TRUE if the value was successfully stored, FALSE otherwise.

To delete a value from the class, call DeleteValue(CString keyname, CString valuename). This function will return TRUE if the value is deleted, FALSE otherwise.

To delete an entire key from the class, call DeleteKey(CString keyname). This function will return TRUE if the key is deleted, FALSE otherwise.

To remove all data stored in the class, call Reset().

Other useful functions are GetNumKeys() which returns the number of keys in the INI file and GetNumValues(CString keyname) which returns the number of values stored in the specified key.

Finally, to write all your stored data to the specified INI file, call WriteFile().

That's it! It is simple.

Email comments to cabadam@tamu.edu.

Updates

  • 5 May 2000

    Updated source and demo files.

  • 2 March 2003

    It has been a long time since I've looked at this article, but as there has been a lot of interest in the non-MFC version of this class, I decided to go ahead and upload it here. As the non-MFC version, rewritten by Shane Hill, contains many more features, I've decided to remove the original MFC class from here. This means a couple things. First - the demo picture at the top of this article is wrong. There are no spaces surrounding the '=' sign. Also, even though the class contains many more features, the basic operation of the class remains the same for the most part. New features include the ability to enumerate existing keys and values and to add comments into the INI file. For the new additions, see the well documented header file.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralINI vs. Reg Key Pin
Newbie28-Apr-00 6:12
Newbie28-Apr-00 6:12 
GeneralRe: INI vs. Reg Key Pin
Brad Bruce28-Apr-00 10:04
Brad Bruce28-Apr-00 10:04 
GeneralRe: INI vs. Reg Key Pin
Adam Clauss29-Apr-00 17:27
Adam Clauss29-Apr-00 17:27 
GeneralRe: INI vs. Reg Key Pin
Daniel29-Apr-00 19:18
Daniel29-Apr-00 19:18 
GeneralRe: INI vs. Reg Key Pin
Newbie30-Apr-00 4:06
Newbie30-Apr-00 4:06 
GeneralRe: INI vs. Reg Key Pin
Steve Quick6-May-00 16:39
Steve Quick6-May-00 16:39 
GeneralRe: INI vs. Reg Key Pin
Jamie Nordmeyer8-May-00 10:30
Jamie Nordmeyer8-May-00 10:30 
GeneralRe: INI vs. Reg Key Pin
Philippe Lhoste11-May-00 0:35
Philippe Lhoste11-May-00 0:35 
Why still use INI files when there is the registry to store program settings?
Very good question!

Microsoft enforced the extended use of the registry, qualifying the INI files as "obsolete".
Note that they "obsoleted" a lot of other techniques, like DDE, but still use them. And indeed, they are still useful.

Let see the advantages of INI files over registry:
- simple to edit, no need to launch Regedit, which can be harmful to other settings if wrongly used.
- simple to backup, before doing an edit, or in case of accidental deletion.
- to uninstall a program which stores its settings in an INI file (in its own directory), if it didn't messed up with the registry for other reasons (like file associations) or sprinkled DLL all over the system, you usually just have to delete its directory: fast and not error prone.
- you can easily copy settings from an install to another (other disk, other computer), with a BAT file or a synchronization program.

I can go on, but it seems a reason good enough to use INI files to store program settings.

One limitation of such files, at least with the Win API, is their size, limited to 64KB if I recall correctly.
Now, this was merely a problem in Windows 3.x, when programs stored their settings in the Win.ini file, which was growing endlessly. It was less problematic when they stored their settings in individual INI files. And to avoid Windows directory clutter, they should store the INI file in the local directory (where the EXE is). See also the uninstall item...

As MS stated, if you have to store big amounts of data, don't use INI files, they are not designed for that. Use instead a separate file, or a database.

Note that Microsoft also recommend this for the registry, and they break this rule themselve. See, for example, the
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder
key (with Active Desktop installed): they store the whole Favorites and Start Menu directory hierarchies with data to order them and store additional data.
The size of this key can be overwhelming: try to export it!

Other side note:
They state that registry keys can't contain spaces. Actually, they can, and MS often puts them in key names...

Well, I hope I wasn't too verbose (I planed to make an article on the subject...) and I was clear (as a French, my English isn't perfect...).

Have a good day.
GeneralRe: INI vs. Reg Key Pin
Peter Sjöström11-May-00 3:23
Peter Sjöström11-May-00 3:23 
GeneralRe: INI vs. Reg Key Pin
Huge6-Jun-00 13:14
Huge6-Jun-00 13:14 
GeneralRe: INI vs. Reg Key Pin
Snacky Pete7-Jul-00 14:57
sussSnacky Pete7-Jul-00 14:57 
Questionoverloading? Pin
teri28-Apr-00 3:12
teri28-Apr-00 3:12 
AnswerRe: overloading? Pin
HP28-Apr-00 4:03
HP28-Apr-00 4:03 
AnswerRe: overloading? Pin
James Curran28-Apr-00 8:34
James Curran28-Apr-00 8:34 
GeneralRe: overloading? Pin
William Kempf28-Apr-00 11:33
sussWilliam Kempf28-Apr-00 11:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.