Click here to Skip to main content
11,437,366 members (36,133 online)
Click here to Skip to main content

CIniFile

, 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

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

Share

About the Author

Cabadam
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
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 
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 
To fix the actual error, either add the line
#include "stdafx.h" to inifile.cpp or turn off precompiled headers for it.

The warning... I don't think it will actually make a difference, should be able to ignore it.

Adam Clauss
cabadam@tamu.edu
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 
GeneralRe: Win98 Problems Pin
Adam Clauss27-Apr-05 12:22
memberAdam Clauss27-Apr-05 12:22 
GeneralLittle improvement Pin
Dennis Kuppens12-Apr-05 10:46
sussDennis Kuppens12-Apr-05 10:46 
QuestionWhat about unicode support? Pin
bebert14-May-04 0:32
memberbebert14-May-04 0:32 
AnswerRe: What about unicode support? Pin
Adam Clauss25-Jun-04 10:31
memberAdam Clauss25-Jun-04 10:31 
GeneralWarnings.... Pin
momo77-May-04 1:35
membermomo77-May-04 1:35 
GeneralRe: Warnings.... Pin
Adam Clauss7-May-04 5:50
memberAdam Clauss7-May-04 5:50 
GeneralWhy not use API Function GetPrivateProfileString Pin
Anonymous23-Mar-04 16:43
sussAnonymous23-Mar-04 16:43 
GeneralRe: Why not use API Function GetPrivateProfileString Pin
Adam Clauss23-Mar-04 16:48
memberAdam Clauss23-Mar-04 16:48 
GeneralMissing syncronization Pin
yokosuna28-Jan-04 1:19
memberyokosuna28-Jan-04 1:19 
Generalmy build error Pin
kjessee31-Oct-03 18:35
memberkjessee31-Oct-03 18:35 
GeneralRe: my build error Pin
Adam Clauss31-Oct-03 18:56
memberAdam Clauss31-Oct-03 18:56 
GeneralRe: my build error Pin
kjessee31-Oct-03 19:42
memberkjessee31-Oct-03 19:42 
GeneralGetNumKeys() Pin
codelion29-Sep-03 8:18
membercodelion29-Sep-03 8:18 
GeneralRe: GetNumValues() Pin
Adam Clauss29-Sep-03 9:36
memberAdam Clauss29-Sep-03 9:36 
GeneralSuggestions Pin
Egi4-Sep-03 23:49
memberEgi4-Sep-03 23:49 
GeneralSuggestions to get rid of warnings Pin
Anonymous7-Jun-03 3:28
sussAnonymous7-Jun-03 3:28 
QuestionHow to convert std:string and MFC:CString? Pin
smurfy24-May-03 3:46
membersmurfy24-May-03 3:46 
AnswerRe: How to convert std:string and MFC:CString? Pin
Adam Clauss24-May-03 4:04
memberAdam Clauss24-May-03 4:04 
GeneralBuild error Pin
dushkin11-May-03 0:04
memberdushkin11-May-03 0:04 
GeneralRe: Build error Pin
Adam Clauss11-May-03 6:34
memberAdam Clauss11-May-03 6:34 
GeneralGreat! Thanks a lot !! Pin
DonGuitar9-May-03 12:20
memberDonGuitar9-May-03 12:20 
GeneralCIniFile class giving me "Unresolved External" errors Pin
tuz23-Apr-03 17:41
membertuz23-Apr-03 17:41 
GeneralRe: CIniFile class giving me "Unresolved External" errors Pin
Adam Clauss23-Apr-03 18:44
memberAdam Clauss23-Apr-03 18:44 
Generalerror C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
vinods921-Mar-03 8:23
membervinods921-Mar-03 8:23 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
Adam Clauss21-Mar-03 8:32
memberAdam Clauss21-Mar-03 8:32 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
vinods921-Mar-03 8:45
membervinods921-Mar-03 8:45 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
Adam Clauss21-Mar-03 8:47
memberAdam Clauss21-Mar-03 8:47 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
Anonymous9-Jul-03 5:53
sussAnonymous9-Jul-03 5:53 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
zaveri16-Jan-04 13:18
memberzaveri16-Jan-04 13:18 
GeneralPlease post old code as well Pin
polynr6913-Mar-03 6:39
memberpolynr6913-Mar-03 6:39 
GeneralRe: Please post old code as well Pin
Adam Clauss14-Mar-03 18:42
memberAdam Clauss14-Mar-03 18:42 
GeneralRe: Please post old code as well Pin
polynr6916-Mar-03 23:43
memberpolynr6916-Mar-03 23:43 
GeneralRe: Please post old code as well Pin
Adam Clauss17-Mar-03 5:20
memberAdam Clauss17-Mar-03 5:20 
QuestionSyntax error on string? Pin
Ironduke10-Mar-03 1:18
memberIronduke10-Mar-03 1:18 
AnswerRe: Syntax error on string? Pin
10-Mar-03 12:07
suss10-Mar-03 12:07 
GeneralRe: Syntax error on string? Pin
elNaxon3-Nov-03 11:02
memberelNaxon3-Nov-03 11:02 
AnswerRe: Syntax error on string? Pin
10-Mar-03 12:32
suss10-Mar-03 12:32 
GeneralRe: Syntax error on string? Pin
Ironduke10-Mar-03 14:02
memberIronduke10-Mar-03 14:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.150428.2 | Last Updated 31 May 2005
Article Copyright 2000 by Cabadam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid