Click here to Skip to main content
12,635,269 members (22,934 online)
Click here to Skip to main content
Add your own
alternative version


86 bookmarked


, 31 May 2005
Rate this:
Please Sign up or sign in to vote.
A class that makes it easy to implement an INI settings file in your applications.

Sample Image - cinifile.gif


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


  • 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.


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


About the Author

Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionUsing of CIniFile For a Commercial Software Pin
Member 837193014-Aug-14 3:34
memberMember 837193014-Aug-14 3:34 
GeneralModified version Pin
free2000fly4-Mar-10 16:13
memberfree2000fly4-Mar-10 16:13 
GeneralRe: Modified version Pin
Michael Stammberger22-Apr-10 21:43
memberMichael Stammberger22-Apr-10 21:43 
GeneralRe: Modified version Pin
free2000fly30-Apr-10 4:09
memberfree2000fly30-Apr-10 4:09 
QuestionPerformance problems Pin
nogga7-Apr-08 1:37
membernogga7-Apr-08 1:37 
Generalline.lentgh == 0 -> problem Pin
rikelme22-Oct-06 22:41
memberrikelme22-Oct-06 22:41 
GeneralSeems don't work on Visual Studio 2005 (VC8) Pin
vfilll7-Oct-06 16:18
membervfilll7-Oct-06 16:18 
GeneralRe: Seems don't work on Visual Studio 2005 (VC8) [modified] Pin
xaoc215-Dec-06 15:21
memberxaoc215-Dec-06 15:21 
GeneralGood work, thanks! Pin
kevin7896-Sep-06 17:02
memberkevin7896-Sep-06 17:02 
GeneralAlow empty line and space before and after = Pin
ciroki14-Mar-06 5:07
memberciroki14-Mar-06 5:07 
Generallearn about GetValue() Pin
Anonymous19-Jul-05 22:20
sussAnonymous19-Jul-05 22:20 
GeneralRe: learn about GetValue() Pin
Adam Clauss21-Jul-05 9:34
memberAdam Clauss21-Jul-05 9:34 
Questiongreat work! but the size of exe file? Pin
Anonymous16-Jul-05 17:01
sussAnonymous16-Jul-05 17:01 
AnswerRe: great work! but the size of exe file? Pin
Adam Clauss21-Jul-05 9:33
memberAdam Clauss21-Jul-05 9:33 
GeneralSimpler Solution: Just Set CWinApp::m_pszProfileName Pin
Mike O'Neill7-Jun-05 8:11
memberMike O'Neill7-Jun-05 8:11 
MFC allows you to set the name of an INI file and thereby bypass the registry while allowing use of the built-in functions of Get/WriteProfileString, Get/WriteProfileInt etc.

The formatting of the INI file is standard and is done automatically by these MFC functions.

All you need to do is to set a value for CWinApp::m_pszProfileName, as described by MSDN at[^]. MSDN also gives sample code, which would be included in CWinApp::InitInstance:
  // ....

  //Free the string allocated by MFC at CWinApp startup.
  //This string is allocated before InitInstance is called.
  //Change the name of the .INI file.
  //The CWinApp destructor will free the memory.


There's nothing more to do. Everything else is handled automatically. Even formatting of the INI file when you use MFC's Get/WriteProfileString, Get/WriteProfileInt etc functions.

GeneralCiniFile MFC compilation errors Pin
foozy_man9-May-05 16:31
memberfoozy_man9-May-05 16:31 
GeneralRe: CiniFile MFC compilation errors Pin
Adam Clauss9-May-05 17:56
memberAdam Clauss9-May-05 17:56 
GeneralRe: CiniFile MFC compilation errors Pin
foozy_man24-May-05 18:50
memberfoozy_man24-May-05 18:50 
GeneralRe: CiniFile MFC compilation errors Pin
Adam Clauss24-May-05 19:00
memberAdam Clauss24-May-05 19:00 
Generalinifile.h is not including in view class of MFC Pin
Nzshi29-Apr-05 0:36
memberNzshi29-Apr-05 0:36 
GeneralRe: inifile.h is not including in view class of MFC Pin
Adam Clauss29-Apr-05 4:18
memberAdam Clauss29-Apr-05 4:18 
GeneralRe: inifile.h is not including in view class of MFC Pin
nazia1231-May-05 19:05
membernazia1231-May-05 19:05 
GeneralRe: inifile.h is not including in view class of MFC Pin
Adam Clauss2-May-05 12:06
memberAdam Clauss2-May-05 12:06 
GeneralRe: inifile.h is not including in view class of MFC Pin
nazia1234-May-05 1:11
membernazia1234-May-05 1:11 
GeneralWin98 Problems Pin
Anonymous27-Apr-05 10:11
sussAnonymous27-Apr-05 10:11 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 31 May 2005
Article Copyright 2000 by Cabadam
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid