📄 csdn_文档中心_一个手工读写ini文件的类(二).htm
字号:
month= tmpDate.getMonth() + 1 ;
if(document.ns)
{
year1=tmpDate.getYear()
year= year1.toString().substr(1,2);
}
else
year= tmpDate.getYear();
document.write(year);
document.write(".");
document.write(month);
document.write(".");
document.write(date);
// -->
</SCRIPT>
</B> </TD></TR>
<TR bgColor=#999999>
<TD colSpan=3 height=1></TD></TR></TBODY></TABLE>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD align=middle bgColor=#fafafa class=td1 vAlign=top width=150><BR>
<SCRIPT src="CSDN_文档中心_一个手工读写INI文件的类(二).files/microsoft.js"></SCRIPT>
</TD>
<TD align=middle width=620>
<TABLE bgColor=#eeeeee border=0 cellPadding=0 cellSpacing=0 width=600>
<TBODY>
<TR bgColor=#ffffff>
<TD align=middle height=10 width=50></TD>
<TD align=right><A href="http://www.csdn.net/">CSDN</A> - <A
href="http://www.csdn.net/develop/">文档中心</A> - <FONT
color=#003399>Visual C++</FONT> </TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399 height=10><FONT
color=#ffffff>标题</FONT></TD>
<TD><B> 一个手工读写INI文件的类(二)</B> bluebohe(原作)
</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
<TD width=500> ini CMap CMapStringToPtr</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
<TABLE border=0 width=600>
<TBODY>
<TR>
<TD align=left><BR>
<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">(读者可以点击<A
href="http://bluebohe.go.nease.net/readini.rar">这里</A>获得源代码,注意解压后将boot.ini拷贝到E:\,以便程序运行找到文件)</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">头文件CfgData.h</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">//
CfgData.h: interface for the CCfgData
class.<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">#if
!defined(AFX_CFGDATA_H__A40CDB9A_0E44_49E6_A460_505D76BA6414__INCLUDED_)<BR>#define
AFX_CFGDATA_H__A40CDB9A_0E44_49E6_A460_505D76BA6414__INCLUDED_</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">#if
_MSC_VER > 1000<BR>#pragma once<BR>#endif // _MSC_VER >
1000</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">class
CCfgData
<BR>{<BR>protected:<BR> //组到配置数据的映射<BR> CMapStringToPtr
m_StrMapMap;<BR> //当前组<BR> CString m_strGroup; <BR>public:<BR> //构造配置数据<BR> CCfgData();<BR> //析构配置数据<BR> virtual
~CCfgData();<BR> //从文件加载配置数据<BR> /*<BR> 参数: LPCTSTR
strFileName 加载文件的名称<BR> 返回值:无<BR> */<BR> void
LoadCfgData(LPCTSTR
strFileName);<BR> //将配置数据保存到文件<BR> /*<BR> 参数: LPCTSTR
strFileName 保存文件的名称<BR> 返回值:成功返回TRUE
失败返回FALSE<BR> */<BR> BOOL SaveCfgData(LPCTSTR
strFileName);<BR> <BR> //将配置数据保存到字符串<BR> /*<BR> 参数: CString*
pstr 要保存的字符串指针<BR> 返回值:成功返回TRUE 失败返回FALSE<BR>
*/<BR> BOOL SaveToStr(CString*
pstr);<BR> //设置当前读取的组<BR> /*<BR> 参数: 当前组名称<BR> 返回值:无<BR> */<BR> void
SetGroup(LPCTSTR
strGroup);<BR> //修改或者添加一条当前组中的数据<BR> /*<BR> 参数: LPCTSTR
strKey 要修改的数据的键值<BR> LPCTSTR
strValue要修改的数据的内容<BR> 返回值:<BR> 备注: 如果当前组在配置数据中存在,则修改或者添加该组对应键值的数据,如果当前组灾配置数据中不存在,则先创建该组<BR>
*/<BR> BOOL SetData(LPCTSTR strKey,LPCTSTR
strValue);<BR> //得到当前组中对应键值中字符串类型的数据<BR> /*<BR> 参数: LPCTSTR
strKey 要得到的数据的键值<BR> LPCTSTR
strValue要得到的数据的内容 <BR> 返回值:找到数据返回TRUE,否则返回FALSE<BR> */<BR> virtual
BOOL GetStrData(LPCTSTR strKey,CString
&strValue);<BR> //得到long型的数据<BR> /*<BR> 参数: LPCTSTR
strKey 要得到的数据的键值<BR> long
lValue 要得到的数据的值 <BR> 返回值:找到数据返回TRUE,否则返回FALSE <BR>
*/<BR> virtual BOOL GetLongData(LPCTSTR strKey,long
&lValue);<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">protected:<BR> //释放配置数据所占用的内存<BR> /*<BR> 参数: 无<BR> 返回值:无<BR>
*/<BR> void RemoveAll();<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">#endif
//
!defined(AFX_CFGDATA_H__A40CDB9A_0E44_49E6_A460_505D76BA6414__INCLUDED_)</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>
<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">源文件CfgData.cpp</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">//
CfgData.cpp: implementation of the CCfgData
class.<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">#include
"stdafx.h"<BR>#include "CfgData.h"</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">#ifdef
_DEBUG<BR>#undef THIS_FILE<BR>static char
THIS_FILE[]=__FILE__;<BR>#define new DEBUG_NEW<BR>#endif</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">//////////////////////////////////////////////////////////////////////<BR>//
Construction/Destruction<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">CCfgData::CCfgData()<BR>{<BR> //初始化配置数据<BR> m_strGroup="设置";<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">CCfgData::~CCfgData()<BR>{<BR> RemoveAll();<BR> <BR>}<BR>//释放配置数据所占用的内存<BR>/*<BR>参数: 无<BR>返回值:无<BR>*/<BR>void
CCfgData::RemoveAll()<BR>{<BR> POSITION
pos=m_StrMapMap.GetStartPosition();<BR> while(pos)<BR> {<BR> CMapStringToString*
pStrMap;<BR> CString
str;<BR> m_StrMapMap.GetNextAssoc(pos,str,(void*&)pStrMap);<BR> if(pStrMap!=NULL)<BR> {<BR> pStrMap->RemoveAll();<BR> //删除掉CString-->
指针映射中的指针<BR> delete
pStrMap;<BR> }<BR> }<BR> m_StrMapMap.RemoveAll();<BR> <BR>}<BR>//从文件加载配置数据<BR>/*<BR>参数: LPCTSTR
strFileName 加载文件的名称<BR>返回值:无<BR>*/<BR>void
CCfgData::LoadCfgData(LPCTSTR strFileName)<BR>{<BR> int
iReadLen=0;<BR> CString str[3];<BR> int
iState=0;//0:正在读入键值 1:正在读入内容 2:正在读入组<BR> unsigned char
ch; //正在读取的字符<BR> //清空配置数据<BR> RemoveAll();<BR> CFile
file;<BR> file.Open(strFileName,
CFile::modeRead);<BR> file.Seek(0,CFile::begin);<BR> str[0]="";//存放键值字符串<BR> str[1]="";//存放内容字符串<BR> str[2]="";//存放组字符串<BR> //字符串到字符串的映射,保存键值和内容<BR> CMapStringToString*
pStrMap=NULL;<BR> do<BR> {<BR> iReadLen=file.Read(&ch,1);<BR> if(iReadLen!=0)<BR> {<BR> //处理中文<BR> if(ch>0x80)//中文<BR> {<BR> str[iState]+=ch;<BR> iReadLen=file.Read(&ch,1);<BR> if(iReadLen!=0)<BR> {<BR> str[iState]+=ch;<BR> }<BR> }else<BR> {<BR> switch(ch)<BR> {<BR> //处理'['读入组字符串<BR> case
'[':<BR> str[0].TrimLeft();<BR> str[0].TrimRight();<BR> str[1].TrimLeft();<BR> str[1].TrimRight();<BR> //确认键值和内容数据为空,否则可能是键值和内容中的符号<BR> if(str[0]==""&&str[1]=="")<BR> {<BR> pStrMap=NULL;<BR> iState=2;<BR> str[2]="";<BR> }else<BR> {<BR> str[iState]+=ch;<BR> }<BR> break;<BR> //处理']'组字符串读入完毕<BR> case
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -