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

Comments
Win98SE Problems
Posted by foozy_man on 04/27/2005 05:51pmI've been using CIniFile on WinXP with no problem, but it's not working on Win98SE. Does anyone know why? And what do I have to change to make it work? Thanks.
ReplyMy contribute
Posted by Legacy on 10/10/2003 12:00amOriginally posted by: Filipe Freitas
ReplyHow to read file of integer ?
Posted by Legacy on 04/26/2003 12:00amOriginally posted by: ldtvn
there is a text file which contains all integer . For example,
Replytest.txt
1 2 3
4 5 6
7 8 9
Please show me a program which can read these number ?
GetSection bug
Posted by Legacy on 04/02/2003 12:00amOriginally posted by: Thomas Moore
Reply
Bug...
Posted by Legacy on 07/18/2002 12:00amOriginally posted by: Francois ENOT
I'm very surprised nobody noticed the following bug which make this class unusable :
in function SetKeyValue :
section is updated this way, which changes the name of the key instead of its value :
Sections[sectionIndex].Keys[keyIndex] = value;
correct line should be :
Sections[sectionIndex].Values[keyIndex] = value;
ReplyThanks for sharing your good code anyway ;-)
Francois
Problem with empty .ini file !
Posted by Legacy on 03/26/2002 12:00amOriginally posted by: gpanda
Adding new section to a empty .ini file will cause problem.
when i add a new section to a new empty .ini file
, cause illegal access violation.
I found some code is wrong:
int CIniFile::AddSection(CString sectionname)
{
int sectionIndex = GetSectionIndex(sectionname);
int sectionsize = Sections.GetSize() ;
sectionsize++;
if(sectionIndex == NOTFOUND)
{
Sections.SetSize(sectionsize) ;
Sections[sectionsize].SectionName =
//^^^^^^^^^^^^
sectionname;
}
return sectionsize;
}
Sections[sectionsize].SectionName should be
Sections[sectionsize-1].SectionName !
Should it's be 0 index?
It seems many statement have the same problem...
(Sorry for my poor English!!)
ReplyBad Code
Posted by Legacy on 03/04/2002 12:00amOriginally posted by: Roger McElfresh
ReplyClass can only be used in MFC programs...
Posted by Legacy on 02/28/2002 12:00amOriginally posted by: Paul McKenzie
This is a good class, however it can only be used in MFC programs because of the CString and CStringArray classes.
Next time, use std::string and vector<string> to make the class useable for MFC and non-MFC Windows programs.
Reply
General remark (apply not only to this author)
Posted by Legacy on 02/28/2002 12:00amOriginally posted by: Ron Hashimshony
Class wizzard adds an include to the CWinApp derived class.
When publishing a source only files, you need to get rid of it first...
In this case this is the line -
Reply#include "TestIni.h"
solution for ini versus registry: apply them both
Posted by Legacy on 02/28/2002 12:00amOriginally posted by: Guy
Hi GIl;
Replyi saw your class and it's nice.
but not supply a lot of features.
one of the major feature that your class should provide is a function that do the follow:
Func(INI_FILE_NAME,PLACE_IN_REGISTRY);
this function should export the ini file into the registery.
and another function:
Func1(PLACE_IN_REGISTRY,INI_FILE_NAME);
and this function import from registry into an ini file.
i think that if you will add this functions you wil have a perfect CIniFile.
if you will do pls let me know
rgrds
GEliyahu