Click here to Skip to main content
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

About the Author

Cabadam
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralModified version Pinmemberfree2000fly4-Mar-10 15:13 
GeneralRe: Modified version PinmemberMichael Stammberger22-Apr-10 20:43 
GeneralRe: Modified version Pinmemberfree2000fly30-Apr-10 3:09 
QuestionPerformance problems Pinmembernogga7-Apr-08 0:37 
Generalline.lentgh == 0 -> problem Pinmemberrikelme22-Oct-06 21:41 
GeneralSeems don't work on Visual Studio 2005 (VC8) Pinmembervfilll7-Oct-06 15:18 
GeneralRe: Seems don't work on Visual Studio 2005 (VC8) [modified] Pinmemberxaoc215-Dec-06 14:21 
GeneralGood work, thanks! Pinmemberkevin7896-Sep-06 16:02 
GeneralAlow empty line and space before and after = Pinmemberciroki14-Mar-06 4:07 
Generallearn about GetValue() PinsussAnonymous19-Jul-05 21:20 
GeneralRe: learn about GetValue() PinmemberAdam Clauss21-Jul-05 8:34 
Questiongreat work! but the size of exe file? PinsussAnonymous16-Jul-05 16:01 
AnswerRe: great work! but the size of exe file? PinmemberAdam Clauss21-Jul-05 8:33 
GeneralSimpler Solution: Just Set CWinApp::m_pszProfileName PinmemberMike O'Neill7-Jun-05 7:11 
GeneralCiniFile MFC compilation errors Pinmemberfoozy_man9-May-05 15:31 
GeneralRe: CiniFile MFC compilation errors PinmemberAdam Clauss9-May-05 16:56 
GeneralRe: CiniFile MFC compilation errors Pinmemberfoozy_man24-May-05 17:50 
GeneralRe: CiniFile MFC compilation errors PinmemberAdam Clauss24-May-05 18:00 
Generalinifile.h is not including in view class of MFC PinmemberNzshi28-Apr-05 23:36 
GeneralRe: inifile.h is not including in view class of MFC PinmemberAdam Clauss29-Apr-05 3:18 
GeneralRe: inifile.h is not including in view class of MFC Pinmembernazia1231-May-05 18:05 
GeneralRe: inifile.h is not including in view class of MFC PinmemberAdam Clauss2-May-05 11:06 
GeneralRe: inifile.h is not including in view class of MFC Pinmembernazia1234-May-05 0:11 
GeneralWin98 Problems PinsussAnonymous27-Apr-05 9:11 
GeneralRe: Win98 Problems PinmemberAdam Clauss27-Apr-05 11:22 
GeneralLittle improvement PinsussDennis Kuppens12-Apr-05 9:46 
QuestionWhat about unicode support? Pinmemberbebert13-May-04 23:32 
AnswerRe: What about unicode support? PinmemberAdam Clauss25-Jun-04 9:31 
GeneralWarnings.... Pinmembermomo77-May-04 0:35 
GeneralRe: Warnings.... PinmemberAdam Clauss7-May-04 4:50 
GeneralWhy not use API Function GetPrivateProfileString PinsussAnonymous23-Mar-04 15:43 
GeneralRe: Why not use API Function GetPrivateProfileString PinmemberAdam Clauss23-Mar-04 15:48 
GeneralMissing syncronization Pinmemberyokosuna28-Jan-04 0:19 
Generalmy build error Pinmemberkjessee31-Oct-03 17:35 
GeneralRe: my build error PinmemberAdam Clauss31-Oct-03 17:56 
GeneralRe: my build error Pinmemberkjessee31-Oct-03 18:42 
GeneralGetNumKeys() Pinmembercodelion29-Sep-03 7:18 
GeneralRe: GetNumValues() PinmemberAdam Clauss29-Sep-03 8:36 
GeneralSuggestions PinmemberEgi4-Sep-03 22:49 
Good class. I am glad that it does not use MFC and there are very helpfull methods. But I do not understand why the parameter of some functions (for example CIniFile::FindKey(string const keyname)) should be std::string? It is not convenient at all. It is much better to have the following prototype - FindKey(LPCTSTR lpszKeyName). Also why there are no versions of GetValueI, GetValueF,GetValueV that also take unsigned const keyID, unsigned const valueID as the method GetValue?
GeneralSuggestions to get rid of warnings PinsussAnonymous7-Jun-03 2:28 
QuestionHow to convert std:string and MFC:CString? Pinmembersmurfy24-May-03 2:46 
AnswerRe: How to convert std:string and MFC:CString? PinmemberAdam Clauss24-May-03 3:04 
GeneralBuild error Pinmemberdushkin10-May-03 23:04 
GeneralRe: Build error PinmemberAdam Clauss11-May-03 5:34 
GeneralGreat! Thanks a lot !! PinmemberDonGuitar9-May-03 11:20 
GeneralCIniFile class giving me "Unresolved External" errors Pinmembertuz23-Apr-03 16:41 
GeneralRe: CIniFile class giving me "Unresolved External" errors PinmemberAdam Clauss23-Apr-03 17:44 
Generalerror C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pinmembervinods921-Mar-03 7:23 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type PinmemberAdam Clauss21-Mar-03 7:32 
GeneralRe: error C2533: 'CIniFile::CIniFile' : constructors not allowed a return type Pinmembervinods921-Mar-03 7:45 

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 | Mobile
Web02 | 2.8.140709.1 | Last Updated 31 May 2005
Article Copyright 2000 by Cabadam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid