Environment: VC++ 6.0 All Windows

I had to build an application that uses an ini file. When looking in CodeGuru I found Adam Clauss' class CIniFile. The problem was that it wasn't very intuitive to work with, so I took his idea of the Sections struct and improved it. I'm using the API functions GetPrivateProfileSectionNames, GetPrivateProfileSection, WritePrivateProfileSection, and WritePrivateProfileString. I believe this class is easier to use.

The class has this functions divided by groups and is very easy to use after creating the object call Open( "c:\MyProjects\test.ini") or by calling SetPath("c:\MyProjects\test.ini") call Open(). After that it is straightforward. You should note that unless you set DoNotWrite(), when you change something in the ini file the class will write the contents.

// operation on the sections and keys
int DeleteSection( CString strsectionname);
int DeleteKey( CString sectionname,
               CString delkey);
int AddSection( CString sectionname);
int AddSection( CStringArray mKeys,
                CStringArray mValues,
                CString sectionname);
void AddKey( CString sectionname,
             CString keyname,
             CString value);

// operations on the ini file
void SetPath( CString path);
BOOL Open( CString path);
BOOL Open();
BOOL Write();
void Reset();
void Close();
void DoNotWrite();

// getters
CString GetKeyValue( CString sctionname,
                     CString keyname);
int GetSection( CStringArray &mKeys,
                CStringArray &mValues,
                CString sectionname);
CString GetSection( int sectionIndex);
int GetSetionsSize(){return Sections.GetSize();}
int GetKeysSize( CString sectionname);

// setters
int SetKeyValue( CString sectionname,
                 CString keyname,
                 CString value);


Download source - 4 Kb

This article was originally published on February 27th, 2002

Most Popular Programming Stories

More for Developers

RSS Feeds

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