dcsimg
December 5, 2016
Hot Topics:

CIniFile - Class for Reading and Writing .INI Files

  • May 5, 2000
  • By Adam Clauss
  • Send Email »
  • More Articles »

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 it's 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 it's 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()
.

Thats it! It is simple.

Here is a complete listing of the functions included in the class:

//default constructor
CIniFile();

//constructor, can specify pathname here instead of 
//using SetPath later
CIniFile(CString inipath);

//default destructor
virtual ~CIniFile();

//sets path of ini file to read and write from
void SetPath(CString newpath);

//reads ini file specified using CIniFile::SetPath()
//returns true if successful, false otherwise
bool ReadFile();

//writes data stored in class to ini file
void WriteFile(); 

//deletes all stored ini data
void Reset();

//returns number of keys currently in the ini
int GetNumKeys();

//returns number of values stored for specified key
int GetNumValues(CString keyname);

//gets value of [keyname] valuename = 
//overloaded to return CString, int, and double,
//returns, or 0 if key/value not found. Sets error 
//member to show problem
CString GetValue(CString keyname, CString valuename); 
int GetValueI(CString keyname, CString valuename); 
double GetValueF(CString keyname, CString valuename);

//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not 
//want it to create the key if it doesn't exist. Returns true 
//if data entered, false otherwise overloaded to accept CString, 
//int, and double
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);

//deletes specified value
//returns true if value existed and deleted, false otherwise 
bool DeleteValue(CString keyname, CString valuename);

//deletes specified key and all values contained within
//returns true if key existed and deleted, false otherwise
bool DeleteKey(CString keyname);

Email comments to pandcc3@comwerx.net

Downloads

Download demo project - 13 Kb
Download source - 4 Kb






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel