int i;
int iPos=0;
CString strKeyValue;
int iMaxCount;
TCHAR chKeyNames[MAX_ALLKEYS]={0}; //总的提出来的字符串
TCHAR chKey[MAX_KEY]={0}; //提出来的一个键名
GetPrivateProfileSection(lpSection,chKeyNames,MAX_ALLKEYS,m_strFileName);
for(i=0;i<MAX_ALLKEYS;i++)
{
if (chKeyNames[i]==0)
if (chKeyNames[i]==chKeyNames[i+1])
break;
}
iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。
arrKey.RemoveAll();//清空原数组
arrValue.RemoveAll();
for(i=0;i<iMaxCount;i++)
{
chKey[iPos++]=chKeyNames[i];
if(chKeyNames[i]==0)
{
strKeyValue=chKey;
arrKey.Add(strKeyValue.Left(strKeyValue.Find("=")));
arrValue.Add(strKeyValue.Mid(strKeyValue.Find("=")+1));
memset(chKey,0,MAX_KEY);
iPos=0;
}
}
return (int)arrKey.GetSize();
}
BOOL CIni::DelAllSections()
{
int nSection;
CStringArray arrSection;
nSection=GetSections(arrSection);
for(int i=0;i<nSection;i++)
{
if(DelSection(arrSection[i]))
return GetLastError();
}
return FALSE;
}
使用方法:
CIni ini("c:\\a.ini");
int n;
/*获得值
TRACE("%s",ini.GetValue("段1","键1"));
*/
/*添加值
ini.SetValue("自定义段","键1","值");
ini.SetValue("自定义段2","键1","值",false);
*/
/*枚举全部段名
CStringArray arrSection;
n=ini.GetSections(arrSection);
for(int i=0;i<n;i++)
TRACE("%s\n",arrSection[i]);
*/
/*枚举全部键名及值
CStringArray arrKey,arrValue;
n=ini.GetKeyValues(arrKey,arrValue,"段1");
for(int i=0;i<n;i++)
TRACE("键:%s\n值:%s\n",arrKey[i],arrValue[i]);
*/
/*删除键值
ini.DelKey("段1","键1");
*/
/*删除段
ini.DelSection("段1");
*/
/*删除全部
ini.DelAllSections();
*/
责任编辑:cyth