C++[类设计] ini配置文件读写类config

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