Click here to Skip to main content
11,572,251 members (76,879 online)
Click here to Skip to main content

CIniFile

, 31 May 2005 347.7K 2.7K 86
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

You may also be interested in...

Comments and Discussions

 
GeneralSuggestions to get rid of warnings Pin
Anonymous7-Jun-03 2:28
sussAnonymous7-Jun-03 2:28 
QuestionHow to convert std:string and MFC:CString? Pin
smurfy24-May-03 2:46
membersmurfy24-May-03 2:46 
AnswerRe: How to convert std:string and MFC:CString? Pin
Adam Clauss24-May-03 3:04
memberAdam Clauss24-May-03 3:04 
GeneralBuild error Pin
dushkin10-May-03 23:04
memberdushkin10-May-03 23:04 
GeneralRe: Build error Pin
Adam Clauss11-May-03 5:34
memberAdam Clauss11-May-03 5:34 
GeneralGreat! Thanks a lot !! Pin
DonGuitar9-May-03 11:20
memberDonGuitar9-May-03 11:20 
GeneralCIniFile class giving me "Unresolved External" errors Pin
tuz23-Apr-03 16:41
membertuz23-Apr-03 16:41 
GeneralRe: CIniFile class giving me "Unresolved External" errors Pin
Adam Clauss23-Apr-03 17:44
memberAdam Clauss23-Apr-03 17:44 
Generalerror C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
vinods921-Mar-03 7:23
membervinods921-Mar-03 7:23 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
Adam Clauss21-Mar-03 7:32
memberAdam Clauss21-Mar-03 7:32 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
vinods921-Mar-03 7:45
membervinods921-Mar-03 7:45 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
Adam Clauss21-Mar-03 7:47
memberAdam Clauss21-Mar-03 7:47 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
Anonymous9-Jul-03 4:53
sussAnonymous9-Jul-03 4:53 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pin
zaveri16-Jan-04 12:18
memberzaveri16-Jan-04 12:18 
Thanx for your help,
It was quite valuble to me.

APRZAVERI
GeneralPlease post old code as well Pin
polynr6913-Mar-03 5:39
memberpolynr6913-Mar-03 5:39 
GeneralRe: Please post old code as well Pin
Adam Clauss14-Mar-03 17:42
memberAdam Clauss14-Mar-03 17:42 
GeneralRe: Please post old code as well Pin
polynr6916-Mar-03 22:43
memberpolynr6916-Mar-03 22:43 
GeneralRe: Please post old code as well Pin
Adam Clauss17-Mar-03 4:20
memberAdam Clauss17-Mar-03 4:20 
QuestionSyntax error on string? Pin
Ironduke10-Mar-03 0:18
memberIronduke10-Mar-03 0:18 
AnswerRe: Syntax error on string? Pin
10-Mar-03 11:07
suss10-Mar-03 11:07 
GeneralRe: Syntax error on string? Pin
elNaxon3-Nov-03 10:02
memberelNaxon3-Nov-03 10:02 
AnswerRe: Syntax error on string? Pin
10-Mar-03 11:32
suss10-Mar-03 11:32 
GeneralRe: Syntax error on string? Pin
Ironduke10-Mar-03 13:02
memberIronduke10-Mar-03 13:02 
GeneralRe: Syntax error on string? Pin
Adam Clauss10-Mar-03 13:22
memberAdam Clauss10-Mar-03 13:22 
GeneralRe: Syntax error on string? Pin
Ironduke11-Mar-03 0:27
memberIronduke11-Mar-03 0:27 
GeneralRe: Syntax error on string? Pin
Adam Clauss11-Mar-03 5:43
memberAdam Clauss11-Mar-03 5:43 
GeneralRe: Syntax error on string? Pin
Ironduke12-Mar-03 8:12
memberIronduke12-Mar-03 8:12 
Generalright click on iniFile.cpp and go to properties. Pin
Exceter10-Apr-03 21:48
memberExceter10-Apr-03 21:48 
GeneralRe: right click on iniFile.cpp and go to properties. Pin
Adam Clauss11-Apr-03 4:17
memberAdam Clauss11-Apr-03 4:17 
GeneralUpdate Submitted Pin
Adam Clauss6-Mar-03 8:37
memberAdam Clauss6-Mar-03 8:37 
GeneralRe: Update Submitted Pin
Jonathan Halterman7-Mar-03 13:54
memberJonathan Halterman7-Mar-03 13:54 
GeneralRe: Update Submitted Pin
Adam Clauss10-Mar-03 13:26
memberAdam Clauss10-Mar-03 13:26 
GeneralGood job! Pin
RobJones20-Dec-02 9:15
memberRobJones20-Dec-02 9:15 
GeneralGreat Class, But Simple Demo Bug Pin
Selevercin29-Sep-02 3:46
memberSelevercin29-Sep-02 3:46 
GeneralKey to CString Pin
NightBlade19-Sep-02 12:23
memberNightBlade19-Sep-02 12:23 
GeneralRe: Key to CString Pin
Adam Clauss19-Sep-02 14:00
memberAdam Clauss19-Sep-02 14:00 
GeneralRe: Key to CString Pin
NightBlade19-Sep-02 15:34
memberNightBlade19-Sep-02 15:34 
GeneralGreat Class Pin
PaulMcD19-Sep-02 2:08
sussPaulMcD19-Sep-02 2:08 
GeneralCompatibility NT / 9X Pin
tut4-Sep-02 4:47
susstut4-Sep-02 4:47 
GeneralRe: Compatibility NT / 9X Pin
Adam Clauss4-Sep-02 5:31
memberAdam Clauss4-Sep-02 5:31 
GeneralRe: Compatibility NT / 9X Pin
J Cardinal18-Nov-02 11:09
memberJ Cardinal18-Nov-02 11:09 
QuestionWhy don't you use existing API calls(Non-MFC)? Pin
Bin8-Jul-02 18:11
memberBin8-Jul-02 18:11 
AnswerRe: Why don't you use existing API calls(Non-MFC)? Pin
Adam Clauss9-Jul-02 0:24
memberAdam Clauss9-Jul-02 0:24 
GeneralRe: Why don't you use existing API calls(Non-MFC)? Pin
Adam Clauss9-Jul-02 0:58
memberAdam Clauss9-Jul-02 0:58 
GeneralRe: Why don't you use existing API calls(Non-MFC)? Pin
Bin27-Aug-02 4:31
memberBin27-Aug-02 4:31 
GeneralRe: Why don't you use existing API calls(Non-MFC)? Pin
Adam Clauss27-Aug-02 5:14
memberAdam Clauss27-Aug-02 5:14 
GeneralRe: Why don't you use existing API calls(Non-MFC)? Pin
Bin27-Aug-02 5:23
memberBin27-Aug-02 5:23 
GeneralRe: Why don't you use existing API calls(Non-MFC)? Pin
Adam Clauss27-Aug-02 5:38
memberAdam Clauss27-Aug-02 5:38 
GeneralRe: Why don't you use existing API calls(Non-MFC)? Pin
Paul Oss1-Nov-02 9:19
memberPaul Oss1-Nov-02 9:19 
GeneralContents of the inifile. Pin
Emma25-Mar-02 4:15
memberEmma25-Mar-02 4:15 

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.150624.2 | Last Updated 31 May 2005
Article Copyright 2000 by Cabadam
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid