CIniEx - Dynamic .INI Class | CodeGuru

CIniEx – Dynamic .INI Class

When creating applications in real world, we need to store simple data to disk. Of course we can use registry to store anything. And it may be faster then file I/O processing. But programs using the registry increases the size of the registry. So after six months later the registry size reaches 30 MB. (You […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 8, 2000
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

When creating applications in real world, we need to store simple data to disk.
Of course we can use registry to store anything. And it may be faster then file
I/O processing. But programs using the registry increases the size of the registry.
So after six months later the registry size reaches 30 MB. (You can find a registry
monitor at
the SysInternals Web site to
see registry’s usage period) And registry size has an upper limit.(Such as 35 MB)

CIniEx Class stores ini file in memory . If you change anything in memory it writes into disk on destructor.

CIniEx has several simple methods to read and write ini files.

First one; Open member function to open new or existing ini file.

BOOL
CIniEx::Open(LPCSTR pFileName,
             BOOL writeWhenChange /*=TRUE*/,
             BOOL createIfNotExist /*=TRUE*/,
             BOOL noCaseSensitive /*=TRUE*/,
             BOOL makeBackup /*=FALSE*/)


pFileName specifies file to open.
writeWhenChange parameter specifies whether to write
the ini data to disk when the data is changed. It’s faster as ini data is stored
in memory. When createIfNotExist=TRUE, then the
ini file is created if it does not exist. noCaseSensitive
option specifies all key matches will not be case sensitive.
And makeBackup parameter specifies whether to
save backup copies of the ini file before saving
it.

Second important member function is SetValue. There are two types.

void CIniEx::SetValue(CString Section,
                      CString Key,
                      CString Value)
void CIniEx::SetValue(CString Key,
                      CString Value)

If you use section names, then use the first function, if not use
second. You can use SetValue like:

CIniEx ini;
CString str;
CString comment;
ini.Open("c:\tmp\new.ini");
ini.SetValue("Settings","MRU1","koray.xls");
ini.SetValue("Settings","MRU2","arda.xls");
ini.SetValue("Settings","MRU3","kamil.xls");
ini.SetValue("Users","Koray","123");
ini.SetValue("Users","Burak","334");
ini.SetValue("MaxUser","50");

Third important member function is GetValue. There are two types for this function as well.

CString CIniEx::GetValue(CString Section,
                         CString Key,
                         CString DefaultValue/*=""*/)
CString CIniEx::GetValue(CString Key)

you can use like:

CIniEx ini;
CString value;
ini.Open(“c:\tmp\new.ini”);
value=ini.GetValue(“MaxUser”);
value=ini.GetValue(“Settings”,”MRU1″);
value=ini.GetValue(“Users”,”root”,”200″);

Other member functions:


	OpenAtExeDirectory
	Opens ini file in the exe file’s directory


	GetKeysInSection
	Gives all sections in the ini file (CStringArray)


	GetSections
	Gives CStringArray of all sections


	GetWriteWhenChange
	Returns TRUE or FALSE(for write when change)


	RemoveKey
	Removes specified key


	RemoveSection
	Removes specified section


	ResetContent
	Deletes all dynamic member variables


	SetBackupFileName
	Sets backup file name for backup operation


	SetWriteWhenChange
	Sets whether to save file when ini data changes


	WriteFile
	Flushes file to disk immediately


Downloads
Download source – 5 Kb
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.