转载:ini配置文件的代码实现

 http://blog.csdn.net/miaoshengwu/archive/2007/05/09/1601511.aspx

<script language="javascript" type="text/javascript"> </script>
<script type="text/javascript"> </script> <script type="text/javascript" src="http://blog.csdn.net/WebResource.axd?d=VT-DPrLTYP31jDF3TQ1B-w2&t=632963535947587500"></script> <script type="text/javascript"> // </script> <script type="text/javascript"> //=0xDC00 && c<0xE000) continue; if (c>=0xD800 && c<0xDC00) { if (i>=wide.length) continue; s= wide.charCodeAt(i++); if (s<0xDC00 || c>=0xDE00) continue; c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000; } // output value if (c<0x80) enc += String.fromCharCode(c); else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F)); else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F)); else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F)); } return enc; } var hexchars = "0123456789ABCDEF"; function toHex(n) { return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF); } var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"; // Mimics function encodeURIComponent in IE 5.5+, Netscape 6+, and Mozilla function encodeURIComponentNew(s) { var s = utf8(s); var c; var enc = ""; for (var i= 0; i -1) { func = new Function(functionPrefix + " " + ev); } else { func = new Function("event", functionPrefix + " " + ev); } eval("control." + eventType + " = func;"); } function Anthem_GetXMLHttpRequest() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else { if (window.Anthem_XMLHttpRequestProgID) { return new ActiveXObject(window.Anthem_XMLHttpRequestProgID); } else { var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; for (var i = 0; i < progIDs.length; ++i) { var progID = progIDs[i]; try { var x = new ActiveXObject(progID); window.Anthem_XMLHttpRequestProgID = progID; return x; } catch (e) { } } } } return null; } function Anthem_CallBack(url, target, id, method, args, clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack) { if (window.Anthem_PreCallBack) { var preCallBackResult = Anthem_PreCallBack(); if (!(typeof preCallBackResult == "undefined" || preCallBackResult)) { if (window.Anthem_CallBackCancelled) { Anthem_CallBackCancelled(); } return null; } } var x = Anthem_GetXMLHttpRequest(); var result = null; if (!x) { result = { "value": null, "error": "NOXMLHTTP" }; Anthem_DebugError(result.error); if (window.Anthem_Error) { Anthem_Error(result); } if (clientCallBack) { clientCallBack(result, clientCallBackArg); } return result; } x.open("POST", url ? url : Anthem_DefaultURL, clientCallBack ? true : false); x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); x.setRequestHeader("Accept-Encoding", "gzip, deflate"); if (clientCallBack) { x.onreadystatechange = function() { if (x.readyState != 4) { return; } Anthem_DebugResponseText(x.responseText); result = Anthem_GetResult(x); if (result.error) { Anthem_DebugError(result.error); if (window.Anthem_Error) { Anthem_Error(result); } } if (updatePageAfterCallBack) { Anthem_UpdatePage(result); } Anthem_EvalClientSideScript(result); clientCallBack(result, clientCallBackArg); x = null; if (window.Anthem_PostCallBack) { Anthem_PostCallBack(); } } } var encodedData = ""; if (target == "Page") { encodedData += "&Anthem_PageMethod=" + method; } else if (target == "MasterPage") { encodedData += "&Anthem_MasterPageMethod=" + method; } else if (target == "Control") { encodedData += "&Anthem_ControlID=" + id.split(":").join("_"); encodedData += "&Anthem_ControlMethod=" + method; } if (args) { for (var argsIndex = 0; argsIndex < args.length; ++argsIndex) { if (args[argsIndex] instanceof Array) { for (var i = 0; i < args[argsIndex].length; ++i) { encodedData += "&Anthem_CallBackArgument" + argsIndex + "=" + Anthem_Encode(args[argsIndex][i]); } } else { encodedData += "&Anthem_CallBackArgument" + argsIndex + "=" + Anthem_Encode(args[argsIndex]); } } } if (updatePageAfterCallBack) { encodedData += "&Anthem_UpdatePage=true"; } if (includeControlValuesWithCallBack) { var form = document.getElementById(Anthem_FormID); if (form != null) { for (var elementIndex = 0; elementIndex < form.length; ++elementIndex) { var element = form.elements[elementIndex]; if (element.name) { var elementValue = null; if (element.nodeName.toUpperCase() == "INPUT") { var inputType = element.getAttribute("type").toUpperCase(); if (inputType == "TEXT" || inputType == "PASSWORD" || inputType == "HIDDEN") { elementValue = element.value; } else if (inputType == "CHECKBOX" || inputType == "RADIO") { if (element.checked) { elementValue = element.value; } } } else if (element.nodeName.toUpperCase() == "SELECT") { if (element.multiple) { elementValue = []; for (var i = 0; i < element.length; ++i) { if (element.options[i].selected) { elementValue.push(element.options[i].value); } } } else if (element.length == 0) { elementValue = null; } else { elementValue = element.value; } } else if (element.nodeName.toUpperCase() == "TEXTAREA") { elementValue = element.value; } if (elementValue instanceof Array) { for (var i = 0; i < elementValue.length; ++i) { encodedData += "&" + element.name + "=" + Anthem_Encode(elementValue[i]); } } else if (elementValue != null) { encodedData += "&" + element.name + "=" + Anthem_Encode(elementValue); } } } // ASP.NET 1.1 won't fire any events if neither of the following // two parameters are not in the request so make sure they're // always in the request. if (typeof form.__VIEWSTATE == "undefined") { encodedData += "&__VIEWSTATE="; } if (typeof form.__EVENTTARGET == "undefined") { encodedData += "&__EVENTTARGET="; } } } if (encodedData.length > 0) { encodedData = encodedData.substring(1); } Anthem_DebugRequestText(encodedData.split("&").join("/n&")); x.send(encodedData); if (!clientCallBack) { Anthem_DebugResponseText(x.responseText); result = Anthem_GetResult(x); if (result.error) { Anthem_DebugError(result.error); if (window.Anthem_Error) { Anthem_Error(result); } } if (updatePageAfterCallBack) { Anthem_UpdatePage(result); } Anthem_EvalClientSideScript(result); if (window.Anthem_PostCallBack) { Anthem_PostCallBack(); } } return result; } function Anthem_GetResult(x) { var result = { "value": null, "error": null }; var responseText = x.responseText; try { result = eval("(" + responseText + ")"); } catch (e) { if (responseText.length == 0) { result.error = "NORESPONSE"; } else { result.error = "BADRESPONSE"; result.responseText = responseText; } } return result; } function Anthem_SetHiddenInputValue(form, name, value) { var input = null; if (form[name]) { input = form[name]; } else { input = document.createElement("input"); input.setAttribute("name", name); input.setAttribute("type", "hidden"); } input.setAttribute("value", value); var parentElement = input.parentElement ? input.parentElement : input.parentNode; if (parentElement == null) { form.appendChild(input); form[name] = input; } } function Anthem_RemoveHiddenInput(form, name) { var input = form[name]; if (input != null && typeof(input) != "undefined") { var parentElement = input.parentElement ? input.parentElement : input.parentNode; if (parentElement != null) { form[name] = null; parentElement.removeChild(input); } } } function Anthem_FireEvent(eventTarget, eventArgument, clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack) { var form = document.getElementById(Anthem_FormID); Anthem_SetHiddenInputValue(form, "__EVENTTARGET", eventTarget); Anthem_SetHiddenInputValue(form, "__EVENTARGUMENT", eventArgument); Anthem_CallBack(null, null, null, null, null, clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack); form.__EVENTTARGET.value = ""; form.__EVENTARGUMENT.value = ""; } function Anthem_UpdatePage(result) { var form = document.getElementById(Anthem_FormID); if (result.viewState) { Anthem_SetHiddenInputValue(form, "__VIEWSTATE", result.viewState); } if (result.viewStateEncrypted) { Anthem_SetHiddenInputValue(form, "__VIEWSTATEENCRYPTED", result.viewStateEncrypted); } if (result.eventValidation) { Anthem_SetHiddenInputValue(form, "__EVENTVALIDATION", result.eventValidation); } if (result.controls) { for (var controlID in result.controls) { var containerID = "Anthem_" + controlID.split("$").join("_") + "__"; var control = document.getElementById(containerID); if (control) { control.innerHTML = result.controls[controlID]; if (result.controls[controlID] == "") { control.style.display = "none"; } else { control.style.display = ""; } } } } if (result.pagescript) { Anthem_LoadPageScript(result, 0); } } // Load each script in order and wait for each one to load before proceeding function Anthem_LoadPageScript(result, index) { if (index < result.pagescript.length) { try { var script = document.createElement('script'); script.type = 'text/javascript'; if (result.pagescript[index].indexOf('src=') == 0) { script.src = result.pagescript[index].substring(4); } else { if (script.canHaveChildren ) { script.appendChild(document.createTextNode(result.pagescript[index])); } else { script.text = result.pagescript[index]; } } var heads = document.getElementsByTagName('head'); if (heads != null && typeof(heads) != "undefined" && heads.length > 0) { var head = heads[0]; // The order that scripts appear is important since later scripts can // redefine a function. Therefore it is important to add every script // to the page and in the same order that they were added on the server. // On the other hand, if we just keep adding scripts the DOM will grow // unnecessarily. This code scans the
element block and removes // previous instances of the identical script. var found = false; for (var child = 0; child < head.childNodes.length; child++) { var control = head.childNodes[child]; if (control.tagName.toUpperCase() == "SCRIPT") { if (script.src.length > 0) { if (script.src == control.src) { found = true; break; } } else if (script.innerHTML.length > 0) { if (script.innerHTML == control.innerHTML) { found = true; break; } } } } if (found) { head.removeChild(control); } // Now we append the new script and move on to the next script. // Note that this is a recursive function. It stops when the // index grows larger than the number of scripts. document.getElementsByTagName('head')[0].appendChild(script); if (typeof script.readyState != "undefined") { script.onreadystatechange = function() { if (script.readyState != "complete" && script.readyState != "loaded") { return; } else { Anthem_LoadPageScript(result, index + 1); } } } else { Anthem_LoadPageScript(result, index + 1); } } } catch (e) { Anthem_DebugError("Error adding page script to head. " + e.name + ": " + e.message); } } } function Anthem_EvalClientSideScript(result) { if (result.script) { for (var i = 0; i < result.script.length; ++i) { try { eval(result.script[i]); } catch (e) { alert("Error evaluating client-side script!/n/nScript: " + result.script[i] + "/n/nException: " + e); } } } } function Anthem_DebugRequestText(text) { } function Anthem_DebugResponseText(text) { } function Anthem_DebugError(text) { } //Fix for bug #1429412, "Reponse callback returns previous response after file push". //see http://sourceforge.net/tracker/index.php?func=detail&aid=1429412&group_id=151897&atid=782464 function Anthem_Clear__EVENTTARGET() { var form = document.getElementById(Anthem_FormID); Anthem_SetHiddenInputValue(form, "__EVENTTARGET", ""); } function Anthem_InvokePageMethod(methodName, args, clientCallBack, clientCallBackArg) { Anthem_Clear__EVENTTARGET(); // fix for bug #1429412 return Anthem_CallBack(null, "Page", null, methodName, args, clientCallBack, clientCallBackArg, true, true); } function Anthem_InvokeMasterPageMethod(methodName, args, clientCallBack, clientCallBackArg) { Anthem_Clear__EVENTTARGET(); // fix for bug #1429412 return Anthem_CallBack(null, "MasterPage", null, methodName, args, clientCallBack, clientCallBackArg, true, true); } function Anthem_InvokeControlMethod(id, methodName, args, clientCallBack, clientCallBackArg) { Anthem_Clear__EVENTTARGET(); // fix for bug #1429412 return Anthem_CallBack(null, "Control", id, methodName, args, clientCallBack, clientCallBackArg, true, true); } function Anthem_PreProcessCallBack( control, e, eventTarget, causesValidation, validationGroup, imageUrlDuringCallBack, textDuringCallBack, enabledDuringCallBack, preCallBackFunction, callBackCancelledFunction, preProcessOut ) { preProcessOut.Enabled = !control.disabled; var preCallBackResult = true; if (preCallBackFunction) { preCallBackResult = preCallBackFunction(control); } if (typeof(preCallBackResult) == "undefined" || preCallBackResult) { var valid = true; if (causesValidation && typeof(Page_ClientValidate) == "function") { valid = Page_ClientValidate(validationGroup); } if (typeof(WebForm_OnSubmit) == "function") { valid = WebForm_OnSubmit(); } if (valid) { var inputType = control.getAttribute("type"); inputType = (inputType == null) ? '' : inputType.toUpperCase(); if (inputType == "IMAGE" && e != null) { var form = document.getElementById(Anthem_FormID); if (e.offsetX) { Anthem_SetHiddenInputValue(form, eventTarget + ".x", e.offsetX); Anthem_SetHiddenInputValue(form, eventTarget + ".y", e.offsetY); } else { Anthem_SetHiddenInputValue(form, eventTarget + ".x", e.clientX - control.offsetLeft + 1); Anthem_SetHiddenInputValue(form, eventTarget + ".y", e.clientY - control.offsetTop + 1); } } if (imageUrlDuringCallBack || textDuringCallBack) { if (control.nodeName.toUpperCase() == "INPUT") { if (inputType == "CHECKBOX" || inputType == "RADIO" || inputType == "TEXT") { preProcessOut.OriginalText = GetLabelText(control.id); SetLabelText(control.id, textDuringCallBack); } else if (inputType == "IMAGE") { if (imageUrlDuringCallBack) { preProcessOut.OriginalText = control.src; control.src = imageUrlDuringCallBack; } else { preProcessOut.ParentElement = control.parentElement ? control.parentElement : control.parentNode; if (preProcessOut.ParentElement) { preProcessOut.OriginalText = preProcessOut.ParentElement.innerHTML; preProcessOut.ParentElement.innerHTML = textDuringCallBack; } } } else if (inputType == "SUBMIT") { preProcessOut.OriginalText = control.value; control.value = textDuringCallBack; } } else if (control.nodeName.toUpperCase() == "SELECT") { preProcessOut.OriginalText = GetLabelText(control.id); SetLabelText(control.id, textDuringCallBack); } else { preProcessOut.OriginalText = control.innerHTML; control.innerHTML = textDuringCallBack; } } control.disabled = (typeof enabledDuringCallBack == "undefined") ? false : !enabledDuringCallBack; return true; } else { return false; } } else { if (callBackCancelledFunction) { callBackCancelledFunction(control); } return false; } } function Anthem_PreProcessCallBackOut() { // Fields this.ParentElement = null; this.OriginalText = ''; this.Enabled = true; } function Anthem_PostProcessCallBack( result, control, eventTarget, clientCallBack, clientCallBackArg, imageUrlDuringCallBack, textDuringCallBack, postCallBackFunction, preProcessOut ) { if (postCallBackFunction) { postCallBackFunction(control); } control.disabled = !preProcessOut.Enabled; var inputType = control.getAttribute("type"); inputType = (inputType == null) ? '' : inputType.toUpperCase(); if (inputType == "IMAGE") { var form = document.getElementById(Anthem_FormID); Anthem_RemoveHiddenInput(form, eventTarget + ".x"); Anthem_RemoveHiddenInput(form, eventTarget + ".y"); } if (imageUrlDuringCallBack || textDuringCallBack) { if (control.nodeName.toUpperCase() == "INPUT") { if (inputType == "CHECKBOX" || inputType == "RADIO" || inputType == "TEXT") { SetLabelText(control.id, preProcessOut.OriginalText); } else if (inputType == "IMAGE") { if (imageUrlDuringCallBack) { control.src = preProcessOut.OriginalText; } else { preProcessOut.ParentElement.innerHTML = preProcessOut.OriginalText; } } else if (inputType == "SUBMIT") { control.value = preProcessOut.OriginalText; } } else if (control.nodeName.toUpperCase() == "SELECT") { SetLabelText(control.id, preProcessOut.OriginalText); } else { control.innerHTML = preProcessOut.OriginalText; } } if (clientCallBack) { clientCallBack(result, clientCallBackArg); } } function Anthem_FireCallBackEvent( control, e, eventTarget, eventArgument, causesValidation, validationGroup, imageUrlDuringCallBack, textDuringCallBack, enabledDuringCallBack, preCallBackFunction, postCallBackFunction, callBackCancelledFunction, includeControlValuesWithCallBack, updatePageAfterCallBack ) { var preProcessOut = new Anthem_PreProcessCallBackOut(); var preProcessResult = Anthem_PreProcessCallBack( control, e, eventTarget, causesValidation, validationGroup, imageUrlDuringCallBack, textDuringCallBack, enabledDuringCallBack, preCallBackFunction, callBackCancelledFunction, preProcessOut ); if (preProcessResult) { Anthem_FireEvent( eventTarget, eventArgument, function(result) { Anthem_PostProcessCallBack( result, control, eventTarget, null, null, imageUrlDuringCallBack, textDuringCallBack, postCallBackFunction, preProcessOut ); }, null, includeControlValuesWithCallBack, updatePageAfterCallBack ); } } function AnthemListControl_OnClick( e, causesValidation, validationGroup, textDuringCallBack, enabledDuringCallBack, preCallBackFunction, postCallBackFunction, callBackCancelledFunction, includeControlValuesWithCallBack, updatePageAfterCallBack ) { var target = e.target || e.srcElement; if (target.nodeName.toUpperCase() == "LABEL" && target.htmlFor != '') return; var eventTarget = target.id.split("_").join("$"); Anthem_FireCallBackEvent( target, e, eventTarget, '', causesValidation, validationGroup, '', textDuringCallBack, enabledDuringCallBack, preCallBackFunction, postCallBackFunction, callBackCancelledFunction, true, true ); } function GetLabelText(id) { var labels = document.getElementsByTagName('label'); for (var i = 0; i < labels.length; i++) { if (labels[i].htmlFor == id) { return labels[i].innerHTML; } } return null; } function SetLabelText(id, text) { var labels = document.getElementsByTagName('label'); for (var i = 0; i < labels.length; i++) { if (labels[i].htmlFor == id) { labels[i].innerHTML = text; return; } } } //]]> </script> <script type="text/javascript" src="http://blog.csdn.net/WebResource.axd?d=1wkz58Z1rsBL-eFHJPld5ERhyO-4qXtUzHp7Q1KwrOE1&t=632963535947587500"></script> <script type="text/javascript"> </script>
  CSDN |  社区 |  技术中心 |  BLOG首页 |  我的首页 |  个人档案 |  联系作者 |  聚合  |   |  搜索 |  登录
  14篇原创: 1篇翻译: 8篇转载: 723次点击: 24个评论: 0个Trackbacks
<script language="javascript" src="http://tag.csdn.net/urltag.aspx" type="text/javascript"></script> 作者tag: mfc、vc++  配置文件的相关操作 CSDN 推荐tag: lpsection  cini  lpkey 

<script type="text/javascript">function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>
//----------------------------------------------------------------------------------
/*
类名:CIni
版本:v2.0

类描述:
本类可以于.ini文件进行操作
*/

文件 1:

#pragma once

#include "afxTempl.h"

class CIni
{
private:
CString m_strFileName;
public:
CIni(CString strFileName):m_strFileName(strFileName)
{
}
public:
//一般性操作:
BOOL SetFileName(LPCTSTR lpFileName); //设置文件名
CString GetFileName(void); //获得文件名
BOOL SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate=true); //设置键值,bCreate是指段名及键名未存在时,是否创建。
CString GetValue(LPCTSTR lpSection, LPCTSTR lpKey); //得到键值.
BOOL DelSection(LPCTSTR strSection); //删除段名
BOOL DelKey(LPCTSTR lpSection, LPCTSTR lpKey); //删除键名


public:
//高级操作:
int GetSections(CStringArray& arrSection); //枚举出全部的段名
int GetKeyValues(CStringArray& arrKey,CStringArray& arrValue,LPCTSTR lpSection); //枚举出一段内的全部键名及值

BOOL DelAllSections();

};

文件 2:

#include "StdAfx.h"
#include "ini.h"

#define MAX_ALLSECTIONS 2048 //全部的段名
#define MAX_SECTION 260 //一个段名长度
#define MAX_ALLKEYS 6000 //全部的键名
#define MAX_KEY 260 //一个键名长度

BOOL CIni::SetFileName(LPCTSTR lpFileName)
{
CFile file;
CFileStatus status;

if(!file.GetStatus(lpFileName,status))
return TRUE;

m_strFileName=lpFileName;
return FALSE;
}

CString CIni::GetFileName(void)
{
return m_strFileName;
}

BOOL CIni::SetValue(LPCTSTR lpSection, LPCTSTR lpKey, LPCTSTR lpValue,bool bCreate)
{
TCHAR lpTemp[MAX_PATH] ={0};

//以下if语句表示如果设置bCreate为false时,当没有这个键名时则返回TRUE(表示出错)
//!*&*none-value*&!* 这是个垃圾字符没有特别意义,这样乱写是防止凑巧相同。
if (!bCreate)
{
GetPrivateProfileString(lpSection,lpKey,"!*&*none-value*&!*",lpTemp,MAX_PATH,m_strFileName);
if(strcmp(lpTemp,"!*&*none-value*&!*")==0)
   return TRUE;
}

if(WritePrivateProfileString(lpSection,lpKey,lpValue,m_strFileName))
return FALSE;
else
return GetLastError();
}

CString CIni::GetValue(LPCTSTR lpSection, LPCTSTR lpKey)
{
DWORD dValue;
TCHAR lpValue[MAX_PATH] ={0};

dValue=GetPrivateProfileString(lpSection,lpKey,"",lpValue,MAX_PATH,m_strFileName);
return lpValue;
}

BOOL CIni::DelSection(LPCTSTR lpSection)
{
if(WritePrivateProfileString(lpSection,NULL,NULL,m_strFileName))
return FALSE;
else
return GetLastError();
}

BOOL CIni::DelKey(LPCTSTR lpSection, LPCTSTR lpKey)
{
if(WritePrivateProfileString(lpSection,lpKey,NULL,m_strFileName))
return FALSE;
else
return GetLastError();
}


int CIni::GetSections(CStringArray& arrSection)
{
/*
本函数基础:
GetPrivateProfileSectionNames - 从 ini 文件中获得 Section 的名称
如果 ini 中有两个 Section: [sec1] 和 [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道  
ini 中有哪些 section 的时候可以用这个 api 来获取名称
*/
int i;  
int iPos=0;  
int iMaxCount;
TCHAR chSectionNames[MAX_ALLSECTIONS]={0}; //总的提出来的字符串
TCHAR chSection[MAX_SECTION]={0}; //存放一个段名。
GetPrivateProfileSectionNames(chSectionNames,MAX_ALLSECTIONS,m_strFileName);

//以下循环,截断到两个连续的0
for(i=0;i<MAX_ALLSECTIONS;i++)
{
if (chSectionNames[i]==0)
   if (chSectionNames[i]==chSectionNames[i+1])
   break;
}

iMaxCount=i+1; //要多一个0号元素。即找出全部字符串的结束部分。
arrSection.RemoveAll();//清空原数组

for(i=0;i<iMaxCount;i++)
{
chSection[iPos++]=chSectionNames[i];
if(chSectionNames[i]==0)
{  
   arrSection.Add(chSection);
   memset(chSection,0,MAX_SECTION);
   iPos=0;
}

}

return (int)arrSection.GetSize();
}

int CIni::GetKeyValues(CStringArray& arrKey,CStringArray& arrValue, LPCTSTR lpSection)
{
/*
本函数基础:
GetPrivateProfileSection- 从 ini 文件中获得一个Section的全部键名及值名
如果ini中有一个段,其下有 "段1=值1" "段2=值2",则返回的是 '段1=值1',0,'段2=值2',0,0 ,当你不知道  
获得一个段中的所有键及值可以用这个。
*/
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();
*/ 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1601511


<script src="http://localhost:82/PromoteIcon.aspx?id=1601511" type="text/javascript"></script> [ 收藏到我的网摘]   miaoshengwu发表于 2007年05月09日 10:38:25
<script type="text/javascript">function hide(){showComment();}</script>
相关文章:



特别推荐:
<script type="text/javascript">document.write(" ");</script>
没有评论。
<script language="javascript" type="text/javascript"> ad_width=468; ad_height=60; adcss=2; unionuser=19; ad_type='j'; count=5; </script> <script type="text/javascript" src="http://tagegg.csdn.net/showads.js" language="javascript"></script> <script src="http://tagegg.csdn.net/b.aspx?action=displayad&unionuser=19&unionurl=http%3A%2F%2Fblog.csdn.net%2Fmiaoshengwu%2Farchive%2F2007%2F05%2F09%2F1601511.aspx&adcss=2&ad_type=j&width=468&height=60&ad_color=&ad_color_border=&count=5" language="JavaScript1.1" type="text/javascript"></script> <script type="text/javascript" src="http://blog.csdn.net/js/showgm.js" language="javascript"></script>

发表评论

 
大名:
网址:
评论 
   
<script src="http://www.csdn.net/common/counter.js" type="text/javascript"></script> width="0" scrolling="no" height="0" frameborder="no" src="http://www.csdn.net/ggmm/dd333.htm" border="0" name="myframe" id="myframe"> <script type="text/javascript"> </script> <script type="text/javascript"> </script> <script language="javascript" type="text/javascript"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值