Click here to Skip to main content
11,480,082 members (67,178 online)
Click here to Skip to main content

CIniFile

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

Comments and Discussions

 
GeneralRe: Why don't you use existing API calls(Non-MFC)? Pin
Adam Clauss27-Aug-02 6:38
memberAdam Clauss27-Aug-02 6:38 
GeneralRe: Why don't you use existing API calls(Non-MFC)? Pin
Paul Oss1-Nov-02 10:19
memberPaul Oss1-Nov-02 10:19 
GeneralContents of the inifile. Pin
Emma25-Mar-02 5:15
memberEmma25-Mar-02 5:15 
GeneralRe: Contents of the inifile. Pin
Adam Clauss25-Mar-02 7:27
memberAdam Clauss25-Mar-02 7:27 
GeneralRe: Contents of the inifile. Pin
Emma26-Mar-02 3:49
memberEmma26-Mar-02 3:49 
GeneralRe: Contents of the inifile. Pin
Adam Clauss26-Mar-02 9:13
memberAdam Clauss26-Mar-02 9:13 
GeneralRe: Contents of the inifile. Pin
Peter, the pumpkin eater5-Aug-02 12:02
sussPeter, the pumpkin eater5-Aug-02 12:02 
GeneralRe: Contents of the inifile. Pin
SREERAMULU7-Mar-03 19:12
memberSREERAMULU7-Mar-03 19:12 
GeneralIf You Are Looking Non-MFC Code... Pin
Adam Clauss4-Mar-02 11:05
memberAdam Clauss4-Mar-02 11:05 
GeneralNon MFC code Pin
Ray Romero15-Feb-02 7:43
memberRay Romero15-Feb-02 7:43 
GeneralSorting and Searech the Keys+Values Pin
Duncan Weir30-Jan-02 0:49
memberDuncan Weir30-Jan-02 0:49 
GeneralRe: Sorting and Searech the Keys+Values Pin
ZhuoGuangSheng7-May-03 7:52
memberZhuoGuangSheng7-May-03 7:52 
GeneralClass if not very good at parsing "non-common" INI's (and it doesn't ignores remarks!!!) Pin
Anonymous10-Sep-01 8:40
memberAnonymous10-Sep-01 8:40 
GeneralRe: Class if not very good at parsing "non-common" INI's (and it doesn't ignores remarks!!!) Pin
Anonymous3-Apr-02 6:30
memberAnonymous3-Apr-02 6:30 
Hi,
if you use the code above then when the output file is written it leaves out the commented lines in the output file. i.e. if you use the ReadFile() method to read in all the data from the file and then you use the WriteFile() method to update the file, it ignores all the commented lines and and doesn't update the file with them. I think it's better to leave them in and just don't update them. They won't be used in teh ini file anyways if they are commented out.


GeneralCIniFile class with MFC Removed Pin
Shane20-Aug-01 22:02
memberShane20-Aug-01 22:02 
GeneralLink errors Pin
dereks19-Aug-01 3:27
memberdereks19-Aug-01 3:27 
GeneralRe: Link errors Pin
Micha19-Aug-01 23:57
memberMicha19-Aug-01 23:57 
GeneralRe: Link errors Pin
Anonymous24-May-02 21:08
memberAnonymous24-May-02 21:08 
GeneralCIniFile and Linux/Unix Pin
Shane16-Aug-01 20:33
memberShane16-Aug-01 20:33 
GeneralRe: CIniFile and Linux/Unix Pin
markkuk16-Aug-01 20:42
membermarkkuk16-Aug-01 20:42 
GeneralRe: CIniFile and Linux/Unix Pin
Shane16-Aug-01 21:13
memberShane16-Aug-01 21:13 
GeneralRe: CIniFile and Linux/Unix Pin
Todd Smith16-Aug-01 21:25
memberTodd Smith16-Aug-01 21:25 
GeneralRe: CIniFile and Linux/Unix Pin
Shane17-Aug-01 22:58
memberShane17-Aug-01 22:58 
GeneralFunction Overloading Pin
Anonymous2-Aug-01 10:13
memberAnonymous2-Aug-01 10:13 
QuestionHow about in C Pin
Anonymous25-May-01 5:52
memberAnonymous25-May-01 5:52 
AnswerRe: How about in C Pin
Carlos Antollini25-May-01 6:13
memberCarlos Antollini25-May-01 6:13 
GeneralRe: How about in C Pin
Anonymous3-Jun-01 17:30
memberAnonymous3-Jun-01 17:30 
GeneralRe: How about in C Pin
Anonymous2-Jul-01 21:20
memberAnonymous2-Jul-01 21:20 
GeneralMemory leak! Pin
Anonymous8-May-01 0:41
memberAnonymous8-May-01 0:41 
GeneralTrimLeft and TrimRight Pin
Phil C23-Feb-01 13:20
memberPhil C23-Feb-01 13:20 
GeneralUpdate INI Pin
Es Jayes17-Nov-00 15:16
memberEs Jayes17-Nov-00 15:16 
GeneralRe: Update INI Pin
Nicolas De Rycke28-Dec-00 5:38
memberNicolas De Rycke28-Dec-00 5:38 
Questiondefault values for CEdit? Pin
Pedro Rocha19-Oct-00 7:15
sussPedro Rocha19-Oct-00 7:15 
AnswerRe: default values for CEdit? Pin
Adam Clauss19-Oct-00 11:10
sussAdam Clauss19-Oct-00 11:10 
GeneralCString vc CEdit Pin
Pedro Rocha19-Oct-00 5:00
sussPedro Rocha19-Oct-00 5:00 
GeneralRe: CString vc CEdit Pin
Adam Clauss19-Oct-00 11:09
sussAdam Clauss19-Oct-00 11:09 
QuestionWhy change the naming convention? Pin
Jason12-May-00 10:58
sussJason12-May-00 10:58 
GeneralNo Enumerator functions Pin
Ben Burnett5-May-00 14:15
sussBen Burnett5-May-00 14:15 
GeneralOops - Noticed Problem in Demo Project Pin
Adam Clauss4-May-00 18:11
sussAdam Clauss4-May-00 18:11 
GeneralNote: Change to Code Pin
Adam Clauss3-May-00 18:10
sussAdam Clauss3-May-00 18:10 
GeneralNeed Opinions: Pin
Adam Clauss3-May-00 16:10
sussAdam Clauss3-May-00 16:10 
GeneralRe: Need Opinions: Pin
Colin Davies6-Jul-02 15:10
memberColin Davies6-Jul-02 15:10 
GeneralHosed INI File Pin
Ed Dixon1-May-00 9:22
sussEd Dixon1-May-00 9:22 
GeneralINI vs Reg Pin
Ed Dixon28-Apr-00 11:54
sussEd Dixon28-Apr-00 11:54 
QuestionWhy not use the Win32 API? Pin
Kelly28-Apr-00 9:18
sussKelly28-Apr-00 9:18 
AnswerRe: Why not use the Win32 API? Pin
danwatt29-Apr-00 18:27
sussdanwatt29-Apr-00 18:27 
GeneralRe: Why not use the Win32 API? Pin
Phil Reynolds8-May-00 6:22
sussPhil Reynolds8-May-00 6:22 
AnswerRe: Why not use the Win32 API? Pin
Jason12-May-00 11:54
sussJason12-May-00 11:54 
AnswerRe: Why not use the Win32 API? Pin
Anonymous26-Jun-01 3:45
memberAnonymous26-Jun-01 3:45 
GeneralINI vs. Reg Key Pin
Newbie28-Apr-00 7:12
sussNewbie28-Apr-00 7:12 

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