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
To retrieve data from the class, use
GetValue or one of its overloads:
CString GetValue(CString keyname, CString valuename);
int GetValueI(CString keyname, CString valuename);
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.
TRUE if the value was successfully stored,
To delete a value from the class, call
DeleteValue(CString keyname, CString valuename). This function will return
TRUE if the value is deleted,
To delete an entire key from the class, call
DeleteKey(CString keyname). This function will return
TRUE if the key is deleted,
To remove all data stored in the class, call
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
That's it! It is simple.
Email comments to firstname.lastname@example.org.
- 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.