Click here to Skip to main content
15,881,248 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 451.1K   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

 
GeneralOops - Noticed Problem in Demo Project Pin
Adam Clauss4-May-00 17:11
Adam Clauss4-May-00 17:11 
GeneralNote: Change to Code Pin
Adam Clauss3-May-00 17:10
Adam Clauss3-May-00 17:10 
GeneralNeed Opinions: Pin
Adam Clauss3-May-00 15:10
Adam Clauss3-May-00 15:10 
GeneralRe: Need Opinions: Pin
ColinDavies6-Jul-02 14:10
ColinDavies6-Jul-02 14:10 
GeneralHosed INI File Pin
Ed Dixon1-May-00 8:22
Ed Dixon1-May-00 8:22 
GeneralINI vs Reg Pin
Ed Dixon28-Apr-00 10:54
Ed Dixon28-Apr-00 10:54 
QuestionWhy not use the Win32 API? Pin
Kelly28-Apr-00 8:18
Kelly28-Apr-00 8:18 
AnswerRe: Why not use the Win32 API? Pin
danwatt29-Apr-00 17:27
danwatt29-Apr-00 17:27 
GeneralRe: Why not use the Win32 API? Pin
Phil Reynolds8-May-00 5:22
Phil Reynolds8-May-00 5:22 
AnswerRe: Why not use the Win32 API? Pin
Jason12-May-00 10:54
Jason12-May-00 10:54 
AnswerRe: Why not use the Win32 API? Pin
26-Jun-01 2:45
suss26-Jun-01 2:45 
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 
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 

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.