📄 csdn_文档中心_一个手工读写ini文件的类(二).htm
字号:
']':<BR> //确认读入的是组的字符串数据<BR> str[2].TrimLeft();<BR> str[2].TrimRight();<BR> if(iState==2&&str[2]!="")<BR> {<BR> iState=0;<BR> //新建一个组下的键值-->内容映射,放入该组<BR> pStrMap=new
CMapStringToString;<BR> m_StrMapMap.SetAt(str[2],pStrMap);<BR> }else<BR> {<BR> str[iState]+=ch;<BR> }<BR> break;<BR> case
'=':<BR> //开始读入内容<BR> iState=1;<BR> str[1]="";<BR> break;<BR> //处理回车和注释<BR> case
';':<BR> case
0x0d:<BR> case
0x0a:<BR> iState=0;<BR> //键值非空<BR> str[0].TrimLeft();<BR> str[0].TrimRight();<BR> str[1].TrimLeft();<BR> str[1].TrimRight();<BR> <BR> if(str[0]!=""&&pStrMap!=NULL)<BR> {<BR> pStrMap->SetAt((LPCTSTR)str[0],(LPCTSTR)str[1]);<BR> }<BR> //处理完清空数据<BR> str[0]="";<BR> str[1]="";<BR> //注释的话继续读入直到文件结束或者碰到回车符号<BR> if(ch==';')<BR> {<BR> while((iReadLen=file.Read(&ch,1))>0)<BR> {<BR> //如果遇到回车符号,终止,并且将当前位置退回<BR> if(ch==0x0d||ch==0x0a)<BR> {<BR> file.Seek(-1,CFile::current);<BR> break;<BR> }<BR> }<BR> }<BR> break;<BR> default:<BR> //普通字符,添加进入相应位置<BR> str[iState]+=ch;<BR> break;<BR> }<BR> }<BR> }<BR> }while(iReadLen!=0);<BR> file.Close();<BR> <BR>}<BR>//设置当前读取的组<BR>/*<BR>参数: 当前组名称<BR>返回值:无<BR>*/</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">void
CCfgData::SetGroup(LPCTSTR
strGroup)<BR>{<BR> m_strGroup=strGroup;<BR>}<BR>//得到当前组中对应键值中字符串类型的数据<BR>/*<BR>参数: LPCTSTR
strKey 要得到的数据的键值<BR>LPCTSTR
strValue要得到的数据的内容 <BR>返回值:找到数据返回TRUE,否则返回FALSE<BR>*/</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">BOOL
CCfgData::GetStrData(LPCTSTR strKey, CString
&strValue)<BR>{<BR> CMapStringToString*
pStrMap=NULL;<BR> //寻找当前组<BR> if(m_StrMapMap.Lookup(m_strGroup,(void*&)pStrMap))<BR> {<BR> if(pStrMap->Lookup(strKey,strValue))<BR> return
TRUE;<BR> return FALSE;<BR> }<BR> return
FALSE;<BR>}<BR>//得到long型的数据<BR>/*<BR>参数: LPCTSTR
strKey 要得到的数据的键值<BR>long
lValue 要得到的数据的值 <BR>返回值:找到数据返回TRUE,否则返回FALSE <BR>*/<BR>BOOL
CCfgData::GetLongData(LPCTSTR strKey, long
&lValue)<BR>{<BR> CString
str;<BR> //得到对应的字符串数据<BR> if(CCfgData::GetStrData(strKey,
str))<BR> {<BR> lValue=atoi((LPCTSTR)str);<BR> return
TRUE;<BR> }<BR> return
FALSE;<BR>}<BR>//修改或者添加一条当前组中的数据<BR>/*<BR>参数: LPCTSTR
strKey 要修改的数据的键值<BR>LPCTSTR
strValue要修改的数据的内容<BR>返回值:<BR>备注: 如果当前组在配置数据中存在,则修改或者添加该组对应键值的数据,如果当前组灾配置数据中不存在,则先创建该组<BR>*/</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">BOOL
CCfgData::SetData(LPCTSTR strKey, LPCTSTR
strValue)<BR>{<BR> CMapStringToString*
pStrMap=NULL;<BR> //如果存在该组,直接加入或者修改<BR> if(m_StrMapMap.Lookup(m_strGroup,(void*&)pStrMap))<BR> {<BR> pStrMap->SetAt(strKey,strValue);<BR> return
TRUE;<BR> }else<BR> {<BR> //创建该组<BR> pStrMap=new
CMapStringToString;<BR> m_StrMapMap.SetAt(m_strGroup,pStrMap);<BR> pStrMap->SetAt(strKey,strValue);<BR> return
TRUE;<BR> }<BR> <BR>}<BR>//将配置数据保存到文件<BR>/*<BR>参数: LPCTSTR
strFileName 保存文件的名称<BR>返回值:成功返回TRUE 失败返回FALSE<BR>*/</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">BOOL
CCfgData::SaveCfgData(LPCTSTR strFileName)<BR>{<BR> CFile
file;<BR> if(!file.Open(strFileName,CFile::modeCreate|CFile::modeWrite))<BR> return
FALSE;<BR> POSITION
pos=m_StrMapMap.GetStartPosition();<BR> char
ch[6]="[]\r\n=";//特殊符号<BR> CString
str[3];<BR> while(pos)<BR> {<BR> CMapStringToString*
pStrMap;<BR> m_StrMapMap.GetNextAssoc(pos,str[2],(void*&)pStrMap);<BR> if(pStrMap!=NULL)<BR> {<BR> //写入组<BR> file.Write(&ch[0],1);<BR> file.Write((LPSTR)(LPCTSTR)str[2],str[2].GetLength());<BR> file.Write(&ch[1],1);<BR> file.Write(&ch[2],2);<BR> POSITION
pos1=pStrMap->GetStartPosition();<BR> while(pos1)<BR> {<BR> //写入键值和内容<BR> pStrMap->GetNextAssoc(pos1,str[0],str[1]);<BR> file.Write((LPSTR)(LPCTSTR)str[0],str[0].GetLength());<BR> file.Write(&ch[4],1);<BR> file.Write((LPSTR)(LPCTSTR)str[1],str[1].GetLength());<BR> file.Write(&ch[2],2);<BR> }<BR> }<BR> }<BR> file.Close();<BR> return
TRUE;<BR>}<BR>//将配置数据保存到字符串<BR>/*<BR>参数: CString*
pstr 要保存的字符串指针<BR>返回值:成功返回TRUE
失败返回FALSE<BR>备注: 程序流程和上面基本相同,不同的保存进入字符串中<BR>*/</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt">BOOL
CCfgData::SaveToStr(CString
*pstr)<BR>{<BR> if(pstr==NULL)return
FALSE;<BR> *pstr="";<BR> POSITION
pos=m_StrMapMap.GetStartPosition();<BR> CString
str[4];<BR> while(pos)<BR> {<BR> CMapStringToString*
pStrMap;<BR> m_StrMapMap.GetNextAssoc(pos,str[2],(void*&)pStrMap);<BR> if(pStrMap!=NULL)<BR> {<BR> str[3]=*pstr;<BR> pstr->Format("%s[%s]\r\n",str[3],str[2]);<BR> POSITION
pos1=pStrMap->GetStartPosition();<BR> while(pos1)<BR> {<BR> pStrMap->GetNextAssoc(pos1,str[0],str[1]);<BR> str[3]=*pstr;<BR> pstr->Format("%s%s=%s\r\n",str[3],str[0],str[1]);<BR> <BR> }<BR> }<BR> }<BR> return
TRUE;<BR> <BR>}</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; FONT-SIZE: 10pt; mso-bidi-font-size: 12.0pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-font-kerning: 0pt"></SPAN> </P><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0
width=770>
<TBODY>
<TR bgColor=#006699>
<TD align=middle bgColor=#006699 id=white><FONT
color=#ffffff>对该文的评论</FONT></TD>
<TD align=middle>
<SCRIPT src="CSDN_文档中心_一个手工读写INI文件的类(二).files/readnum.htm"></SCRIPT>
</TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16
hspace=1 src="CSDN_文档中心_一个手工读写INI文件的类(二).files/ico_pencil.gif" width=16>
</SPAN> csbison <I>(2004-4-7 19:41:08)</I> </TD></TR>
<TR>
<TD bgColor=#ffffff colSpan=3 width=532><BR>嗯,不错
<BR></TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TH bgColor=#006699 id=white><FONT
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<SCRIPT language=javascript>
<!--
function isEmpty(s)
{
return ((s == null) || (s.length == 0))
}
function fubmitok()
{
if (isEmpty(document.add_critique.Critique_Content.value))
{
alert('评论不能为空!!!!') ;
return false;
}
document.add_critique.submit();
}
//-->
</SCRIPT>
<DIV align=center>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD>
<FORM action=Critique_Sql.asp method=post name=add_critique><INPUT
name=Critique_State type=hidden value=add> 评论人:xyj0323
评论:<BR> <TEXTAREA cols=104 name=Critique_Content rows=8></TEXTAREA><BR> <INPUT name=ubmit onclick=javascript:fubmitok(); type=button value=发表评论>
<INPUT name=Topic_id type=hidden value=26342> <INPUT name=From type=hidden
value=/Develop/Build_Article.asp?id=26342>
</FORM></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
<TBODY>
<TR align=middle>
<TD height=10 vAlign=bottom><A
href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
border=0 height=48 src="CSDN_文档中心_一个手工读写INI文件的类(二).files/biaoshi.gif"
width=40></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.net, Inc. All rights
reserved</FONT></TD></TR>
<TR>
<TD height=15></TD>
<TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -