1. //in Config.h
  2. #pragma once
  3. #include <windows.h>
  4. #include <shlwapi.h>
  5. #pragma comment(lib,"shlwapi")
  6. #include <Tchar.h>
  7. class CConfig
  8. {
  9. public:
  10. CConfig(LPTSTR strFileName=NULL,LPTSTR strFilePath=NULL);
  11. virtual ~CConfig(void);
  12. private:
  13. TCHAR m_szFileName[MAX_PATH];
  14. TCHAR m_szFilePath[MAX_PATH];
  15. TCHAR m_szAppName[MAX_PATH];
  16. const TCHAR *m_pszFileExt;
  17. const TCHAR *m_pszFileDir;
  18. public:
  19. bool AddKey(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
  20. bool DeleteKey(LPCTSTR strDelKeyName,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
  21. bool DeleteSection(LPCTSTR strDelSectionName=NULL,LPCTSTR strFilePath=NULL);
  22. bool ReadKeyValue(LPCTSTR strKeyName,OUT LPTSTR strKeyVal, LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
  23. bool ReadKeyValue(LPCTSTR strKeyName, OUT int &nKeyVal, LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
  24. bool ModifyKeyValue(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
  25. int GetSectionCount(LPCTSTR strFilePath=NULL);
  26. int GetKeyCount(LPCTSTR strSectionName=NULL, LPCTSTR strFilePath=NULL);
  27. };
  28. //in Config.cpp
  29. #include "Config.h"
  30. /************************************************************************************************************/
  31. /*                                                                                                          */
  32. /* Function name : CConfig                                                                              */
  33. /* Description   : Create and initialize a CConfig Object.The parameter strFileName indicated the name
  34. of the ini file,it must not contain extension .ini.And strFilePath indicated the path
  35. of the ini file which to be created and stored.If strFileName or strFilePath is NULL,then
  36. use the app's name or current directory as default ini file name or path.If application
  37. calls this member function to create it's ini file at default path,then the ini file will
  38. be stored unifily in the subdirectory "...\config\".
  39. Attention:This function does not create an actual ini file.                                                                     */
  40. /*                                                                                                          */
  41. /************************************************************************************************************/
  42. CConfig::CConfig(LPTSTR strFileName,LPTSTR strFilePath)
  43. : m_pszFileExt(_T(".ini")),m_pszFileDir(_T("\\config"))
  44. {
  45. memset(m_szFileName,0,MAX_PATH);
  46. memset(m_szFilePath,0,MAX_PATH);
  47. memset(m_szAppName,0,MAX_PATH);
  48. ::GetModuleFileName(NULL,m_szFilePath,MAX_PATH);
  49. ::GetFileTitle(m_szFilePath,m_szFileName,MAX_PATH);
  50. ::PathRemoveExtension(m_szFileName);
  51. _tcscpy_s(m_szAppName,MAX_PATH,m_szFileName);
  52. if( strFilePath!=NULL)
  53. {
  54. /*if strFilePath is valid,copy it to m_szFilePath and handle it to a directory*/
  55. if(::PathIsDirectory(strFilePath))
  56. {
  57. _tcscpy_s(m_szFilePath,MAX_PATH,strFilePath);
  58. ::PathRemoveBackslash(m_szFilePath);
  59. ::PathAddBackslash(m_szFilePath);
  60. }
  61. else//use a default directory
  62. {
  63. ::PathRemoveFileSpec(m_szFilePath);
  64. ::PathAddBackslash(m_szFilePath);
  65. }
  66. }
  67. else
  68. {
  69. ::PathRemoveFileSpec(m_szFilePath);
  70. _tcscat_s(m_szFilePath,MAX_PATH,m_pszFileDir);
  71. if(!::PathFileExists(m_szFilePath))
  72. {
  73. ::CreateDirectory(m_szFilePath,NULL);
  74. }
  75. ::PathAddBackslash(m_szFilePath);
  76. if(strFileName !=NULL)
  77. {
  78. _tcscpy_s(m_szFileName,MAX_PATH,strFileName);
  79. }
  80. }
  81. _tcscat_s(m_szFileName,MAX_PATH,m_pszFileExt);
  82. _tcscat_s(m_szFilePath,MAX_PATH,m_szFileName);
  83. }
  84. CConfig::~CConfig(void)
  85. {
  86. }
  87. /************************************************************************************************************/
  88. /*                                                                                                          */
  89. /* Function name : AddKey                                                                               */
  90. /* Description   : Create a key-value pair with format "strKeyName=strKeyValue" in the specified section by
  91. strSectionName.If strSectionName is NULL,then use the app's name as default section
  92. name to be added into. If the section specified by strSectionName does not exist, it is
  93. created. The strKeyValue will be modified if strKeyName already exists.This function
  94. creates an actual ini file.
  95. Return true if the function succeed,otherwise false.
  96. */
  97. /*                                                                                                          */
  98. /************************************************************************************************************/
  99. bool CConfig::AddKey(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName,LPCTSTR strFilePath)
  100. {
  101. LPCTSTR szSectionName;
  102. LPCTSTR szFilePath;
  103. if(strSectionName==NULL)
  104. szSectionName=m_szAppName;
  105. else
  106. szSectionName=strSectionName;
  107. if(strFilePath==NULL)
  108. szFilePath=m_szFilePath;
  109. else
  110. szFilePath=strFilePath;
  111. if(::WritePrivateProfileString(szSectionName,strKeyName,strKeyValue,szFilePath))
  112. return true;
  113. else
  114. return false;
  115. }
  116. /************************************************************************************************************/
  117. /*                                                                                                          */
  118. /* Function name : DeleteKey                                                                                */
  119. /* Description   : Delete a key and it's value from the specified section.If the parameter strSectionName is
  120. NULL,then delete the section with app's name.
  121. Return true if the function succeed,otherwise false.                                                                        */
  122. /*                                                                                                          */
  123. /************************************************************************************************************/
  124. bool CConfig::DeleteKey(LPCTSTR strDelKeyName,LPCTSTR strSectionName,LPCTSTR strFilePath)
  125. {
  126. LPCTSTR szSectionName;
  127. LPCTSTR szFilePath;
  128. if(strSectionName==NULL)
  129. szSectionName=m_szAppName;
  130. else
  131. szSectionName=strSectionName;
  132. if(strFilePath==NULL)
  133. szFilePath=m_szFilePath;
  134. else
  135. szFilePath=strFilePath;
  136. if(::WritePrivateProfileString(szSectionName,strDelKeyName,NULL,szFilePath))
  137. return true;
  138. else
  139. return false;
  140. }
  141. /************************************************************************************************************/
  142. /*                                                                                                          */
  143. /* Function name : DeleteSection                                                                            */
  144. /* Description   : Delete a specified section and all it's associated contents from the initialization file.
  145. If the parameter strDelSectionName is no offered,then delete the section with app's name.
  146. Return true if the function succeed,otherwise false.
  147. /*                                                                                                          */
  148. /************************************************************************************************************/
  149. bool CConfig::DeleteSection(LPCTSTR strDelSectionName,LPCTSTR strFilePath)
  150. {
  151. LPCTSTR szSectionName;
  152. LPCTSTR szFilePath;
  153. if(strDelSectionName==NULL)
  154. szSectionName=m_szAppName;
  155. else
  156. szSectionName=strDelSectionName;
  157. if(strFilePath==NULL)
  158. szFilePath=m_szFilePath;
  159. else
  160. szFilePath=strFilePath;
  161. if(::WritePrivateProfileString(szSectionName,NULL,NULL,szFilePath))
  162. return true;
  163. else
  164. return false;
  165. }
  166. /************************************************************************************************************/
  167. /*                                                                                                          */
  168. /* Function name : ReadKeyValue                                                                             */
  169. /* Description   : Retrieves the value of strKeyName as String into the buffer specified by parameter
  170. strKeyVal.If the parameter strSectionName and strFilePath is no offered,then use the
  171. app's name as default section and ini file to be search.Return true if the function
  172. succeed,otherwise false,and the parameter strKeyVal will be set to NULL.
  173. /*                                                                                                          */
  174. /************************************************************************************************************/
  175. bool CConfig::ReadKeyValue(LPCTSTR strKeyName, LPTSTR strKeyVal, LPCTSTR strSectionName,LPCTSTR strFilePath)
  176. {
  177. LPCTSTR szSectionName;
  178. LPCTSTR szFilePath;
  179. if(strSectionName==NULL)
  180. szSectionName=m_szAppName;
  181. else
  182. szSectionName=strSectionName;
  183. if(strFilePath==NULL)
  184. szFilePath=m_szFilePath;
  185. else
  186. szFilePath=strFilePath;
  187. ::GetPrivateProfileString(szSectionName,strKeyName,NULL,strKeyVal,_tcslen(strKeyVal),szFilePath);
  188. if(_tcscmp(strKeyVal,_T(""))==0)
  189. return false;
  190. else
  191. return true;
  192. }
  193. /************************************************************************************************************/
  194. /*                                                                                                          */
  195. /* Function name : ReadKeyValue                                                                             */
  196. /* Description   : Retrieves the value of strKeyName as Int into the buffer specified by parameter
  197. strKeyVal.If the parameter strSectionName and strFilePath is no offered,then use the
  198. app's name as default section and ini file to be search.Return true if the function
  199. succeed,otherwise false,and the parameter strKeyVal will be set to -1.*/
  200. /*                                                                                                          */
  201. /************************************************************************************************************/
  202. bool CConfig::ReadKeyValue(LPCTSTR strKeyName, int &nKeyVal, LPCTSTR strSectionName,LPCTSTR strFilePath)
  203. {
  204. LPCTSTR szSectionName;
  205. LPCTSTR szFilePath;
  206. if(strSectionName==NULL)
  207. szSectionName=m_szAppName;
  208. else
  209. szSectionName=strSectionName;
  210. if(strFilePath==NULL)
  211. szFilePath=m_szFilePath;
  212. else
  213. szFilePath=strFilePath;
  214. nKeyVal=::GetPrivateProfileInt(szSectionName,strKeyName,-1,szFilePath);
  215. if(-1 !=nKeyVal)
  216. return true;
  217. else
  218. return false;
  219. }
  220. /************************************************************************************************************/
  221. /*                                                                                                          */
  222. /* Function name : ModifyKeyValue                                                                           */
  223. /* Description   : Replace the key value of strKeyName with strKeyValue .If the parameter strSectionName
  224. and strFilePath is no offered,then use the app's name as default section and ini file to
  225. be search.Return true if the function succeed,otherwise false.                          */
  226. /*                                                                                                          */
  227. /************************************************************************************************************/
  228. bool CConfig::ModifyKeyValue(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName,LPCTSTR strFilePath)
  229. {
  230. LPCTSTR szSectionName;
  231. LPCTSTR szFilePath;
  232. if(strSectionName==NULL)
  233. szSectionName=m_szAppName;
  234. else
  235. szSectionName=strSectionName;
  236. if(strFilePath==NULL)
  237. szFilePath=m_szFilePath;
  238. else
  239. szFilePath=strFilePath;
  240. ::WritePrivateProfileString(szSectionName,strKeyName,NULL,szFilePath);
  241. if(::WritePrivateProfileString(szSectionName,strKeyName,strKeyValue,szFilePath))
  242. return true;
  243. else
  244. return false;
  245. }
  246. /************************************************************************************************************/
  247. /*                                                                                                          */
  248. /* Function name : GetSectionCount                                                                          */
  249. /* Description   : Retrieves the number of all sections in the initialization file specified by strFilePath.
  250. if strFilePath is NULL,then use the app's name as default ini file to be search.If the
  251. function succeed,the return value is not -1.                                            */
  252. /*                                                                                                          */
  253. /************************************************************************************************************/
  254. int CConfig::GetSectionCount(LPCTSTR strFilePath)
  255. {
  256. TCHAR szItem[MAX_PATH]={0};
  257. LPCTSTR szFilePath;
  258. if(strFilePath==NULL)
  259. szFilePath=m_szFilePath;
  260. else
  261. szFilePath=strFilePath;
  262. int nRet=::GetPrivateProfileSectionNames(szItem,MAX_PATH,szFilePath);
  263. int nSecCount=0;
  264. if(nRet !=MAX_PATH-2)
  265. {
  266. for(int i=0;i<MAX_PATH;i++)
  267. {
  268. if(szItem[i]==0 && szItem[i+1]!=0)
  269. {
  270. nSecCount++;
  271. }
  272. else if(szItem[i]==0 && szItem[i+1]==0)
  273. {
  274. nSecCount++;
  275. break;
  276. }
  277. }
  278. }
  279. else
  280. nSecCount=-1;
  281. return nSecCount;
  282. }
  283. /************************************************************************************************************/
  284. /*                                                                                                          */
  285. /* Function name : GetKeyCount                                                                          */
  286. /* Description   : Retrieves the number of all key in the section specified by strSectionName in the
  287. initialization file specified by strFilePath.If strSectionName and strFilePath is NULL,
  288. then use the app's name as default section and ini file to be search.If the
  289. function succeed,the return value is not -1.                                            */
  290. /*                                                                                                          */
  291. /************************************************************************************************************/
  292. int CConfig::GetKeyCount(LPCTSTR strSectionName, LPCTSTR strFilePath)
  293. {
  294. TCHAR szItem[MAX_PATH]={0};
  295. LPCTSTR szSectionName;
  296. LPCTSTR szFilePath;
  297. if(strSectionName==NULL)
  298. szSectionName=m_szAppName;
  299. else
  300. szSectionName=strSectionName;
  301. if(strFilePath==NULL)
  302. szFilePath=m_szFilePath;
  303. else
  304. szFilePath=strFilePath;
  305. int nRet=::GetPrivateProfileSection(szSectionName,szItem,MAX_PATH,szFilePath);
  306. int nSecCount=0;
  307. if(nRet !=MAX_PATH-2)
  308. {
  309. for(int i=0;i<MAX_PATH;i++)
  310. {
  311. if(szItem[i]==0 && szItem[i+1]!=0)
  312. {
  313. nSecCount++;
  314. }
  315. else if(szItem[i]==0 && szItem[i+1]==0)
  316. {
  317. nSecCount++;
  318. break;
  319. }
  320. }
  321. }
  322. else
  323. nSecCount=-1;
  324. return nSecCount;
  325. }
  326. // in main function
  327. #include <iostream>
  328. #include "Config.h"
  329. int main()
  330. {
  331. CConfig MyConfig;
  332. MyConfig.AddKey(_T("ID"),_T("123456"));
  333. MyConfig.AddKey(_T("账户"),_T("123456"),_T("MySection"));
  334. MyConfig.AddKey(_T("余额"),_T("654321"),_T("MySection"));
  335. //MyConfig.DeleteKey(_T("ID"));
  336. //MyConfig.DeleteSection();
  337. LPCTSTR key=_T("ID");
  338. LPCTSTR key1=_T("账户");
  339. LPCTSTR key2=_T("余额");
  340. TCHAR szBuf[MAX_PATH]={0};
  341. LPTSTR pstrValue=szBuf;
  342. int nValue=0;
  343. MyConfig.ReadKeyValue(key,nValue);
  344. std::cout << "ID=" << nValue << std::endl;
  345. MyConfig.ReadKeyValue(key1,nValue,_T("MySection"));
  346. std::cout << "账户=" << nValue << std::endl;
  347. MyConfig.ReadKeyValue(key2,nValue,_T("MySection"));
  348. std::cout << "余额=" << nValue << std::endl;
  349. MyConfig.ModifyKeyValue(_T("余额"),_T("923475632"),_T("MySection"));
  350. MyConfig.ReadKeyValue(key2,nValue,_T("MySection"));
  351. std::cout << "余额=" << nValue << std::endl;
  352. std::cout << MyConfig.GetKeyCount(_T("MySection")) << std::endl;
  353. /*CConfig MyConfig2;
  354. MyConfig2.AddKey(_T("新增记录"),_T("4571498"));
  355. MyConfig2.AddKey(_T("新增记录"),_T("0775-4571498"));*/
  356. getchar();
  357. return 0;
  358. }

C++[类设计] ini配置文件读写类config的更多相关文章

  1. C# INI配置文件读写类

    ini是一种很古老的配置文件,C#操作ini文件借助windows底层ini操作函数,使用起来很方便: public class IniHelper { [DllImport("kernel ...

  2. 【个人使用.Net类库】(1)INI配置文件操作类

    开发接口程序时,对于接口程序配置的IP地址.端口等都需要是可配置的,而在Win Api原生实现了INI文件的读写操作,因此只需要调用Win Api中的方法即可操作INI配置文件,关键代码就是如何调用W ...

  3. [IO] C# INI文件读写类与源码下载 (转载)

    /// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...

  4. c#通用配置文件读写类(xml,ini,json)

    .NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...

  5. c#通用配置文件读写类与格式转换(xml,ini,json)

    .NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...

  6. C# ini配置文件操作类

    /// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary&g ...

  7. C# 如何实现完整的INI文件读写类

    作者: 魔法软糖 日期: 2020-02-27 引言 ************************************* .ini 文件是Initialization File的缩写,即配置文 ...

  8. Qt的QSettings类和.ini文件读写

    Detailed Description QSettings类提供了持久的跨平台的应用程序设置.用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表 ...

  9. 【转】ini载入保存类,操作INI配置文件方便的很

    /****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:\> ...

随机推荐

  1. iOS扫描二维码(系统方法)

    步骤如下: 1.导入AVFoundation框架,引入<AVFoundation/AVFoundation.h> 2.设置一个用于显示扫描的view 3.实例化AVCaptureSessi ...

  2. .net中XML的创建01(传统方法)

    XML传统的创建: 传统的创建主要是依据XmlDocument的对象展开的,通过XmlDocument对象可以创建元素(XmlElement).属性(XmlAttribute)以及文本节点(Creat ...

  3. Chrome扩展与用户隐私

    转载自https://www.imququ.com/post/chrome-extensions-and-user-privacy.html   Google Chrome浏览器应该早就是大家的默认了 ...

  4. (转)Android Studio系列教程一下载与安装 背景Android Studio VS Eclipse准备下载创建HelloWorld项目

    背景 相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开发推出的新的开发工具,目前很多开源项目都已经在采用,Goo ...

  5. javascript 16/1/14随记

    1.想在一个事件或者函数之后,触发某个事件. var flag=false //定义一个全局变量 function aku(){ if(flag){ } } sizemousedown=functio ...

  6. JS中 submit提交与Form表单里的onsubmit的调用问题?

    最近在开发中遇到了表单提交前验证的问题,用一个普通的button按钮代替submit按钮,在提交前触发这个button的onclick事件,在其事件中触发form的submit事件.问题出现了: &l ...

  7. mysql 表格中的数据量过大,修改数据库字段信息会花费很长的时间

    遇到几百万的数据操作遇到很多问题,比如分区,比如分表,sql语句的效率问题.努力学习好mysql优化还是很有必要的.

  8. 【行为型】TemplateMethod模式

    模板方法意图是为算法定义好骨架结构,并且其中的某些步骤延迟到子类实现.该模式算是较为简单的一种设计模式.在实际中,应用也较为频繁.模式的类关系图参考如下: 模式的编码结构参考如下: namespace ...

  9. 转:玩转Google开源C++单元测试框架Google Test系列

    转自http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html 前段时间学习和了解了下Google的开源C++单元测试框架Google ...

  10. 移动app测试浅析

    移动App测试浅析 1. 移动App测试的现状及其挑战 移动互联网走到今天,App寡头化的趋势已经越来越明显,同时用户的口味越来越高,这对移动App开发者提出了更高的要求.几年前可能你有一个创意,随便 ...