Click here to Skip to main content
15,860,972 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 448.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

 
GeneralRe: Please post old code as well Pin
Adam Clauss17-Mar-03 4:20
Adam Clauss17-Mar-03 4:20 
QuestionSyntax error on string? Pin
Ironduke10-Mar-03 0:18
Ironduke10-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
Member 6808023-Nov-03 10:02
Member 6808023-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
Ironduke10-Mar-03 13:02 
GeneralRe: Syntax error on string? Pin
Adam Clauss10-Mar-03 13:22
Adam Clauss10-Mar-03 13:22 
GeneralRe: Syntax error on string? Pin
Ironduke11-Mar-03 0:27
Ironduke11-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::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char>
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > 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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(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::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char>
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > 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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(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::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char>
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > 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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(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::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char>
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > 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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(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::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char>
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > 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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(void)'
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std:
:basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,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::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<c
har,std::char_traits<char>,std::allocator<char> > > >::~std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >(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
Adam Clauss11-Mar-03 5:43 
GeneralRe: Syntax error on string? Pin
Ironduke12-Mar-03 8:12
Ironduke12-Mar-03 8:12 
Generalright click on iniFile.cpp and go to properties. Pin
Exceter10-Apr-03 21:48
Exceter10-Apr-03 21:48 
GeneralRe: right click on iniFile.cpp and go to properties. Pin
Adam Clauss11-Apr-03 4:17
Adam Clauss11-Apr-03 4:17 
GeneralUpdate Submitted Pin
Adam Clauss6-Mar-03 8:37
Adam Clauss6-Mar-03 8:37 
GeneralRe: Update Submitted Pin
Jonathan Halterman7-Mar-03 13:54
Jonathan Halterman7-Mar-03 13:54 
GeneralRe: Update Submitted Pin
Adam Clauss10-Mar-03 13:26
Adam Clauss10-Mar-03 13:26 
GeneralGood job! Pin
RobJones20-Dec-02 9:15
RobJones20-Dec-02 9:15 
GeneralGreat Class, But Simple Demo Bug Pin
Selevercin29-Sep-02 3:46
Selevercin29-Sep-02 3:46 
GeneralKey to CString Pin
NightBlade19-Sep-02 12:23
NightBlade19-Sep-02 12:23 
GeneralRe: Key to CString Pin
Adam Clauss19-Sep-02 14:00
Adam Clauss19-Sep-02 14:00 
GeneralRe: Key to CString Pin
NightBlade19-Sep-02 15:34
NightBlade19-Sep-02 15:34 
GeneralGreat Class Pin
paulmcd19-Sep-02 2:08
paulmcd19-Sep-02 2:08 
GeneralCompatibility NT / 9X Pin
tut.monat4-Sep-02 4:47
tut.monat4-Sep-02 4:47 
GeneralRe: Compatibility NT / 9X Pin
Adam Clauss4-Sep-02 5:31
Adam Clauss4-Sep-02 5:31 
GeneralRe: Compatibility NT / 9X Pin
Member 9618-Nov-02 11:09
Member 9618-Nov-02 11:09 
QuestionWhy don't you use existing API calls(Non-MFC)? Pin
Abin8-Jul-02 18:11
Abin8-Jul-02 18: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.