Click here to Skip to main content
11,704,474 members (57,339 online)
Click here to Skip to main content

CIniFile

, 31 May 2005 349.1K 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

 
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 
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 
I now get thisFrown | :(

-------------------Configuration: MovieBase - Win32 Debug--------------------
Compiling...
MainFrm.cpp
c:\program files\microsoft visual studio\myprojects\moviebase\inifile.h(115) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > const *,std::basic_string,std::allocator
>,std::basic_string,std::allocator > const &,std::basic_string,std::allocator > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > *,std::basic_string,std::allocator >,std:
:basic_string,std::allocator > &,std::basic_string,std::allocator > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
MovieBase.cpp
c:\program files\microsoft visual studio\myprojects\moviebase\inifile.h(115) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > const *,std::basic_string,std::allocator
>,std::basic_string,std::allocator > const &,std::basic_string,std::allocator > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > *,std::basic_string,std::allocator >,std:
:basic_string,std::allocator > &,std::basic_string,std::allocator > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
MovieBaseDoc.cpp
c:\program files\microsoft visual studio\myprojects\moviebase\inifile.h(115) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > const *,std::basic_string,std::allocator
>,std::basic_string,std::allocator > const &,std::basic_string,std::allocator > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > *,std::basic_string,std::allocator >,std:
:basic_string,std::allocator > &,std::basic_string,std::allocator > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
MovieBaseView.cpp
c:\program files\microsoft visual studio\myprojects\moviebase\inifile.h(115) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > const *,std::basic_string,std::allocator
>,std::basic_string,std::allocator > const &,std::basic_string,std::allocator > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > *,std::basic_string,std::allocator >,std:
:basic_string,std::allocator > &,std::basic_string,std::allocator > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
NewDatabaseDlg.cpp
c:\program files\microsoft visual studio\myprojects\moviebase\inifile.h(115) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > const *,std::basic_string,std::allocator
>,std::basic_string,std::allocator > const &,std::basic_string,std::allocator > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator,std::allocator > *,std::basic_string,std::allocator >,std:
:basic_string,std::allocator > &,std::basic_string,std::allocator > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(58) : while compiling class-template member function '__thiscall std::vector,std::allocator >,std::allocator,std::allocator > > >::~std::vector,std::allocator >,std::allocator,std::allocator > > >(void)'
iniFile.cpp
c:\program files\microsoft visual studio\myprojects\moviebase\inifile.cpp(32) : warning C4005: 'iniEOL' : macro redefinition
c:\program files\microsoft visual studio\myprojects\moviebase\inifile.cpp(30) : see previous definition of 'iniEOL'
c:\program files\microsoft visual studio\myprojects\moviebase\inifile.cpp(488) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Generating Code...
Error executing cl.exe.

MovieBase.exe - 1 error(s), 16 warning(s)
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 
GeneralRe: Contents of the inifile. Pin
Adam Clauss25-Mar-02 6:27
memberAdam Clauss25-Mar-02 6:27 
GeneralRe: Contents of the inifile. Pin
Emma26-Mar-02 2:49
memberEmma26-Mar-02 2:49 
GeneralRe: Contents of the inifile. Pin
Adam Clauss26-Mar-02 8:13
memberAdam Clauss26-Mar-02 8:13 
GeneralRe: Contents of the inifile. Pin
Peter, the pumpkin eater5-Aug-02 11:02
sussPeter, the pumpkin eater5-Aug-02 11:02 
GeneralRe: Contents of the inifile. Pin
SREERAMULU7-Mar-03 18:12
memberSREERAMULU7-Mar-03 18:12 
GeneralIf You Are Looking Non-MFC Code... Pin
Adam Clauss4-Mar-02 10:05
memberAdam Clauss4-Mar-02 10:05 
GeneralNon MFC code Pin
Ray Romero15-Feb-02 6:43
memberRay Romero15-Feb-02 6:43 
GeneralSorting and Searech the Keys+Values Pin
Duncan Weir29-Jan-02 23:49
memberDuncan Weir29-Jan-02 23:49 
GeneralRe: Sorting and Searech the Keys+Values Pin
ZhuoGuangSheng7-May-03 6:52
memberZhuoGuangSheng7-May-03 6:52 
GeneralClass if not very good at parsing "non-common" INI's (and it doesn't ignores remarks!!!) Pin
Anonymous10-Sep-01 7:40
memberAnonymous10-Sep-01 7:40 
GeneralRe: Class if not very good at parsing "non-common" INI's (and it doesn't ignores remarks!!!) Pin
Anonymous3-Apr-02 5:30
memberAnonymous3-Apr-02 5:30 

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