CIniFile - Class for Reading and Writing .INI Files

Wednesday Mar 5th 2003 by Adam Clauss
Share:

Very easy-to-use class to handle reading and writing .INI files. (The demo project and source code were updated.)

.

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().

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);

Downloads

Download demo project and source - 8KB

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved