C++[类设计] ini配置文件读写类config
- //in Config.h
- #pragma once
- #include <windows.h>
- #include <shlwapi.h>
- #pragma comment(lib,"shlwapi")
- #include <Tchar.h>
- class CConfig
- {
- public:
- CConfig(LPTSTR strFileName=NULL,LPTSTR strFilePath=NULL);
- virtual ~CConfig(void);
- private:
- TCHAR m_szFileName[MAX_PATH];
- TCHAR m_szFilePath[MAX_PATH];
- TCHAR m_szAppName[MAX_PATH];
- const TCHAR *m_pszFileExt;
- const TCHAR *m_pszFileDir;
- public:
- bool AddKey(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
- bool DeleteKey(LPCTSTR strDelKeyName,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
- bool DeleteSection(LPCTSTR strDelSectionName=NULL,LPCTSTR strFilePath=NULL);
- bool ReadKeyValue(LPCTSTR strKeyName,OUT LPTSTR strKeyVal, LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
- bool ReadKeyValue(LPCTSTR strKeyName, OUT int &nKeyVal, LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
- bool ModifyKeyValue(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName=NULL,LPCTSTR strFilePath=NULL);
- int GetSectionCount(LPCTSTR strFilePath=NULL);
- int GetKeyCount(LPCTSTR strSectionName=NULL, LPCTSTR strFilePath=NULL);
- };
- //in Config.cpp
- #include "Config.h"
- /************************************************************************************************************/
- /* */
- /* Function name : CConfig */
- /* Description : Create and initialize a CConfig Object.The parameter strFileName indicated the name
- of the ini file,it must not contain extension .ini.And strFilePath indicated the path
- of the ini file which to be created and stored.If strFileName or strFilePath is NULL,then
- use the app's name or current directory as default ini file name or path.If application
- calls this member function to create it's ini file at default path,then the ini file will
- be stored unifily in the subdirectory "...\config\".
- Attention:This function does not create an actual ini file. */
- /* */
- /************************************************************************************************************/
- CConfig::CConfig(LPTSTR strFileName,LPTSTR strFilePath)
- : m_pszFileExt(_T(".ini")),m_pszFileDir(_T("\\config"))
- {
- memset(m_szFileName,0,MAX_PATH);
- memset(m_szFilePath,0,MAX_PATH);
- memset(m_szAppName,0,MAX_PATH);
- ::GetModuleFileName(NULL,m_szFilePath,MAX_PATH);
- ::GetFileTitle(m_szFilePath,m_szFileName,MAX_PATH);
- ::PathRemoveExtension(m_szFileName);
- _tcscpy_s(m_szAppName,MAX_PATH,m_szFileName);
- if( strFilePath!=NULL)
- {
- /*if strFilePath is valid,copy it to m_szFilePath and handle it to a directory*/
- if(::PathIsDirectory(strFilePath))
- {
- _tcscpy_s(m_szFilePath,MAX_PATH,strFilePath);
- ::PathRemoveBackslash(m_szFilePath);
- ::PathAddBackslash(m_szFilePath);
- }
- else//use a default directory
- {
- ::PathRemoveFileSpec(m_szFilePath);
- ::PathAddBackslash(m_szFilePath);
- }
- }
- else
- {
- ::PathRemoveFileSpec(m_szFilePath);
- _tcscat_s(m_szFilePath,MAX_PATH,m_pszFileDir);
- if(!::PathFileExists(m_szFilePath))
- {
- ::CreateDirectory(m_szFilePath,NULL);
- }
- ::PathAddBackslash(m_szFilePath);
- if(strFileName !=NULL)
- {
- _tcscpy_s(m_szFileName,MAX_PATH,strFileName);
- }
- }
- _tcscat_s(m_szFileName,MAX_PATH,m_pszFileExt);
- _tcscat_s(m_szFilePath,MAX_PATH,m_szFileName);
- }
- CConfig::~CConfig(void)
- {
- }
- /************************************************************************************************************/
- /* */
- /* Function name : AddKey */
- /* Description : Create a key-value pair with format "strKeyName=strKeyValue" in the specified section by
- strSectionName.If strSectionName is NULL,then use the app's name as default section
- name to be added into. If the section specified by strSectionName does not exist, it is
- created. The strKeyValue will be modified if strKeyName already exists.This function
- creates an actual ini file.
- Return true if the function succeed,otherwise false.
- */
- /* */
- /************************************************************************************************************/
- bool CConfig::AddKey(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName,LPCTSTR strFilePath)
- {
- LPCTSTR szSectionName;
- LPCTSTR szFilePath;
- if(strSectionName==NULL)
- szSectionName=m_szAppName;
- else
- szSectionName=strSectionName;
- if(strFilePath==NULL)
- szFilePath=m_szFilePath;
- else
- szFilePath=strFilePath;
- if(::WritePrivateProfileString(szSectionName,strKeyName,strKeyValue,szFilePath))
- return true;
- else
- return false;
- }
- /************************************************************************************************************/
- /* */
- /* Function name : DeleteKey */
- /* Description : Delete a key and it's value from the specified section.If the parameter strSectionName is
- NULL,then delete the section with app's name.
- Return true if the function succeed,otherwise false. */
- /* */
- /************************************************************************************************************/
- bool CConfig::DeleteKey(LPCTSTR strDelKeyName,LPCTSTR strSectionName,LPCTSTR strFilePath)
- {
- LPCTSTR szSectionName;
- LPCTSTR szFilePath;
- if(strSectionName==NULL)
- szSectionName=m_szAppName;
- else
- szSectionName=strSectionName;
- if(strFilePath==NULL)
- szFilePath=m_szFilePath;
- else
- szFilePath=strFilePath;
- if(::WritePrivateProfileString(szSectionName,strDelKeyName,NULL,szFilePath))
- return true;
- else
- return false;
- }
- /************************************************************************************************************/
- /* */
- /* Function name : DeleteSection */
- /* Description : Delete a specified section and all it's associated contents from the initialization file.
- If the parameter strDelSectionName is no offered,then delete the section with app's name.
- Return true if the function succeed,otherwise false.
- /* */
- /************************************************************************************************************/
- bool CConfig::DeleteSection(LPCTSTR strDelSectionName,LPCTSTR strFilePath)
- {
- LPCTSTR szSectionName;
- LPCTSTR szFilePath;
- if(strDelSectionName==NULL)
- szSectionName=m_szAppName;
- else
- szSectionName=strDelSectionName;
- if(strFilePath==NULL)
- szFilePath=m_szFilePath;
- else
- szFilePath=strFilePath;
- if(::WritePrivateProfileString(szSectionName,NULL,NULL,szFilePath))
- return true;
- else
- return false;
- }
- /************************************************************************************************************/
- /* */
- /* Function name : ReadKeyValue */
- /* Description : Retrieves the value of strKeyName as String into the buffer specified by parameter
- strKeyVal.If the parameter strSectionName and strFilePath is no offered,then use the
- app's name as default section and ini file to be search.Return true if the function
- succeed,otherwise false,and the parameter strKeyVal will be set to NULL.
- /* */
- /************************************************************************************************************/
- bool CConfig::ReadKeyValue(LPCTSTR strKeyName, LPTSTR strKeyVal, LPCTSTR strSectionName,LPCTSTR strFilePath)
- {
- LPCTSTR szSectionName;
- LPCTSTR szFilePath;
- if(strSectionName==NULL)
- szSectionName=m_szAppName;
- else
- szSectionName=strSectionName;
- if(strFilePath==NULL)
- szFilePath=m_szFilePath;
- else
- szFilePath=strFilePath;
- ::GetPrivateProfileString(szSectionName,strKeyName,NULL,strKeyVal,_tcslen(strKeyVal),szFilePath);
- if(_tcscmp(strKeyVal,_T(""))==0)
- return false;
- else
- return true;
- }
- /************************************************************************************************************/
- /* */
- /* Function name : ReadKeyValue */
- /* Description : Retrieves the value of strKeyName as Int into the buffer specified by parameter
- strKeyVal.If the parameter strSectionName and strFilePath is no offered,then use the
- app's name as default section and ini file to be search.Return true if the function
- succeed,otherwise false,and the parameter strKeyVal will be set to -1.*/
- /* */
- /************************************************************************************************************/
- bool CConfig::ReadKeyValue(LPCTSTR strKeyName, int &nKeyVal, LPCTSTR strSectionName,LPCTSTR strFilePath)
- {
- LPCTSTR szSectionName;
- LPCTSTR szFilePath;
- if(strSectionName==NULL)
- szSectionName=m_szAppName;
- else
- szSectionName=strSectionName;
- if(strFilePath==NULL)
- szFilePath=m_szFilePath;
- else
- szFilePath=strFilePath;
- nKeyVal=::GetPrivateProfileInt(szSectionName,strKeyName,-1,szFilePath);
- if(-1 !=nKeyVal)
- return true;
- else
- return false;
- }
- /************************************************************************************************************/
- /* */
- /* Function name : ModifyKeyValue */
- /* Description : Replace the key value of strKeyName with strKeyValue .If the parameter strSectionName
- and strFilePath is no offered,then use the app's name as default section and ini file to
- be search.Return true if the function succeed,otherwise false. */
- /* */
- /************************************************************************************************************/
- bool CConfig::ModifyKeyValue(LPCTSTR strKeyName, LPCTSTR strKeyValue,LPCTSTR strSectionName,LPCTSTR strFilePath)
- {
- LPCTSTR szSectionName;
- LPCTSTR szFilePath;
- if(strSectionName==NULL)
- szSectionName=m_szAppName;
- else
- szSectionName=strSectionName;
- if(strFilePath==NULL)
- szFilePath=m_szFilePath;
- else
- szFilePath=strFilePath;
- ::WritePrivateProfileString(szSectionName,strKeyName,NULL,szFilePath);
- if(::WritePrivateProfileString(szSectionName,strKeyName,strKeyValue,szFilePath))
- return true;
- else
- return false;
- }
- /************************************************************************************************************/
- /* */
- /* Function name : GetSectionCount */
- /* Description : Retrieves the number of all sections in the initialization file specified by strFilePath.
- if strFilePath is NULL,then use the app's name as default ini file to be search.If the
- function succeed,the return value is not -1. */
- /* */
- /************************************************************************************************************/
- int CConfig::GetSectionCount(LPCTSTR strFilePath)
- {
- TCHAR szItem[MAX_PATH]={0};
- LPCTSTR szFilePath;
- if(strFilePath==NULL)
- szFilePath=m_szFilePath;
- else
- szFilePath=strFilePath;
- int nRet=::GetPrivateProfileSectionNames(szItem,MAX_PATH,szFilePath);
- int nSecCount=0;
- if(nRet !=MAX_PATH-2)
- {
- for(int i=0;i<MAX_PATH;i++)
- {
- if(szItem[i]==0 && szItem[i+1]!=0)
- {
- nSecCount++;
- }
- else if(szItem[i]==0 && szItem[i+1]==0)
- {
- nSecCount++;
- break;
- }
- }
- }
- else
- nSecCount=-1;
- return nSecCount;
- }
- /************************************************************************************************************/
- /* */
- /* Function name : GetKeyCount */
- /* Description : Retrieves the number of all key in the section specified by strSectionName in the
- initialization file specified by strFilePath.If strSectionName and strFilePath is NULL,
- then use the app's name as default section and ini file to be search.If the
- function succeed,the return value is not -1. */
- /* */
- /************************************************************************************************************/
- int CConfig::GetKeyCount(LPCTSTR strSectionName, LPCTSTR strFilePath)
- {
- TCHAR szItem[MAX_PATH]={0};
- LPCTSTR szSectionName;
- LPCTSTR szFilePath;
- if(strSectionName==NULL)
- szSectionName=m_szAppName;
- else
- szSectionName=strSectionName;
- if(strFilePath==NULL)
- szFilePath=m_szFilePath;
- else
- szFilePath=strFilePath;
- int nRet=::GetPrivateProfileSection(szSectionName,szItem,MAX_PATH,szFilePath);
- int nSecCount=0;
- if(nRet !=MAX_PATH-2)
- {
- for(int i=0;i<MAX_PATH;i++)
- {
- if(szItem[i]==0 && szItem[i+1]!=0)
- {
- nSecCount++;
- }
- else if(szItem[i]==0 && szItem[i+1]==0)
- {
- nSecCount++;
- break;
- }
- }
- }
- else
- nSecCount=-1;
- return nSecCount;
- }
- // in main function
- #include <iostream>
- #include "Config.h"
- int main()
- {
- CConfig MyConfig;
- MyConfig.AddKey(_T("ID"),_T("123456"));
- MyConfig.AddKey(_T("账户"),_T("123456"),_T("MySection"));
- MyConfig.AddKey(_T("余额"),_T("654321"),_T("MySection"));
- //MyConfig.DeleteKey(_T("ID"));
- //MyConfig.DeleteSection();
- LPCTSTR key=_T("ID");
- LPCTSTR key1=_T("账户");
- LPCTSTR key2=_T("余额");
- TCHAR szBuf[MAX_PATH]={0};
- LPTSTR pstrValue=szBuf;
- int nValue=0;
- MyConfig.ReadKeyValue(key,nValue);
- std::cout << "ID=" << nValue << std::endl;
- MyConfig.ReadKeyValue(key1,nValue,_T("MySection"));
- std::cout << "账户=" << nValue << std::endl;
- MyConfig.ReadKeyValue(key2,nValue,_T("MySection"));
- std::cout << "余额=" << nValue << std::endl;
- MyConfig.ModifyKeyValue(_T("余额"),_T("923475632"),_T("MySection"));
- MyConfig.ReadKeyValue(key2,nValue,_T("MySection"));
- std::cout << "余额=" << nValue << std::endl;
- std::cout << MyConfig.GetKeyCount(_T("MySection")) << std::endl;
- /*CConfig MyConfig2;
- MyConfig2.AddKey(_T("新增记录"),_T("4571498"));
- MyConfig2.AddKey(_T("新增记录"),_T("0775-4571498"));*/
- getchar();
- return 0;
- }
C++[类设计] ini配置文件读写类config的更多相关文章
- C# INI配置文件读写类
ini是一种很古老的配置文件,C#操作ini文件借助windows底层ini操作函数,使用起来很方便: public class IniHelper { [DllImport("kernel ...
- 【个人使用.Net类库】(1)INI配置文件操作类
开发接口程序时,对于接口程序配置的IP地址.端口等都需要是可配置的,而在Win Api原生实现了INI文件的读写操作,因此只需要调用Win Api中的方法即可操作INI配置文件,关键代码就是如何调用W ...
- [IO] C# INI文件读写类与源码下载 (转载)
/// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...
- c#通用配置文件读写类(xml,ini,json)
.NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...
- c#通用配置文件读写类与格式转换(xml,ini,json)
.NET下编写程序的时候经常会使用到配置文件.配置文件格式通常有xml.ini.json等几种,操作不同类型配置文件需要使用不同的方法,操作较为麻烦.特别是针对同时应用不同格式配置文件的时候,很容易引 ...
- C# ini配置文件操作类
/// <summary> /// INI文件操作类 /// </summary> public class IniFileHelper { /// <summary&g ...
- C# 如何实现完整的INI文件读写类
作者: 魔法软糖 日期: 2020-02-27 引言 ************************************* .ini 文件是Initialization File的缩写,即配置文 ...
- Qt的QSettings类和.ini文件读写
Detailed Description QSettings类提供了持久的跨平台的应用程序设置.用户通常期望应用程序记住它的设置(窗口大小.位置等)所有会话.这些信息通常存储在Windows系统注册表 ...
- 【转】ini载入保存类,操作INI配置文件方便的很
/****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:\> ...
随机推荐
- Asp.net IsPostBack
Page.IsPostBack是一个标志:当前请求是否第一次打开.调用方法为:Page.IsPostBack或者IsPostBack或者this.IsPostBack或者this.Page.IsPos ...
- java学习——网络编程UDP
UDP 将数据及源和目的封装成数据包中,不需要建立连接 每个数据报的大小限制在64k内 因无连接,是不可靠协议 不需要建立连接,速度快 TCP 建立连接,形成传输数据的通道 在连接中进行大数据量传输 ...
- 【Usaco2008 Mar】土地购买
[题目描述] 农夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000,000; ...
- 【USACO 2.1.3】三值的排序
[题目描述] 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排 ...
- TalkingDataGame SDK在android Lua上的使用
千呼万唤使出来...终于开始更新lua版本的内容了,之前一直有这方面的计划,由于公司业务比较多,一直比较忙-见谅.. 费话不多说,直接上内容.. 整体来讲,先是先建议看一下之前关于cocos2dx上的 ...
- gdb调试带参数程序(转:笑笑小白,cnblog http://www.cnblogs.com/rosesmall/archive/2012/04/10/2440514.html)
一般来说GDB主要调试的是C/C++的程序.要调试C/C++的程序,首先在编译时,我们必须要 把调试信息加到可执行文件中.使用编译 器(cc/gcc/g++)的 -g 参数可以做到这一点.如: > ...
- Delphi-CompareStr 函数
函数名称 CompareStr 所在单元 System.SysUtils 函数原型 function CompareStr(const S1, S2: string): Integer; 函数功能 比 ...
- FCKEditor使用说明
1.基本设置 先看看效果是什么样的: 效果图: 那么为什么说是FCKeditor的冰冷之心呢?这不是哗众取宠,主要是说它使用起来有点麻烦,下文就详细说明如何搞定这玩意儿. 1.FCKedito ...
- laravel框架——保存用户登陆信息(session)
public function inlog(Request $request) { //获取表单提交的数据 $input = $request->all(); //根本获取的数据去数据库中查询 ...
- 至芯FPGA培训中心-1天FPGA设计集训(赠送FPGA开发板)
至芯FPGA培训中心-1天FPGA设计集训(赠送开发板) 开课时间2014年5月3日 课程介绍 FPGA设计初级培训班是针对于FPGA设计技术初学者的课程.课程不仅是对FPGA结构资源和设计流程的描述 ...