c++ windows 使用 i配置文件

来源:互联网 发布:java界面编程实例 编辑:程序博客网 时间:2024/06/14 01:37

INI

 

(文件扩展名)

 
INI文件格式是某些平台或软件上的配置文件的非正式标准,以节(section)和键(key)构成,常用于微软Windows操作系统中。这种配置文件的文件扩展名多为INI,故名。
INI是英文“初始化”(initialization)的缩写。正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置。
微软提供了三个函数 来使用        写入  读取 string  读取   int    
WritePrivateProfileString   写入 
GetPrivateProfileString 读取 string 
GetPrivateProfileInt 读取 int     
我看了 一下 并没有读取  小数的 方法   于是 自己实现了一个 类 封装 了一下       
头文件

//////////////////////////////////////////////ini配置文件class MyIni{public:MyIni(const TCHAR * strFilePath = TEXT(""));/*** \brief 设置打开文件路径* \param  传递一个路径的字符串*/VOID SetPath(const TCHAR * strPath);/*** \brief 得到一个文件路径* \return 返回文件路径*/const TCHAR * GetPath()const;/** * \brief  写入INI * \param AppName   名称  * \param KeyName项名 * \param str内容 * \return  成功返回true */BOOL WriteFile(const TCHAR* AppName, const TCHAR* KeyName,const TCHAR* str);/** * \brief  读取一个字符串 * \param AppName   名称 * \param KeyName  项名 * \param str读取的内容 * \param nSize预留的大小 * \return  成功返回true */BOOL ReadFileString(const TCHAR* AppName, const TCHAR* KeyName,TCHAR* str,  DWORD   nSize);size_t ReadFileInt(const TCHAR* AppName, const TCHAR* KeyName);double ReadFileDouble(const TCHAR* AppName, const TCHAR* KeyName);~MyIni();private:BOOL _IsFilePath()const;TCHAR *m_strFilePath;//文件路径};


源文件 
MyIni::MyIni(const TCHAR* strFilePath) :m_strFilePath(nullptr){SetPath(strFilePath);}void MyIni::SetPath(const TCHAR* strPath){if (strPath == nullptr || strPath == NULL){throw WindowsException(_T("路径不能为nullptr"));}SIZE_T  nstrLen = _tcslen(strPath) + sizeof(TCHAR);if (m_strFilePath != nullptr)delete[]m_strFilePath;m_strFilePath = new TCHAR[nstrLen];_tcscpy_s(m_strFilePath, nstrLen, strPath);}const TCHAR* MyIni::GetPath() const{return m_strFilePath;}BOOL MyIni::WriteFile(const TCHAR* AppName, const TCHAR* KeyName, const TCHAR* str){if(!_IsFilePath())throw WindowsException(_T("写Ini需要先设置路径"));  BOOL bRet =WritePrivateProfileString(AppName, KeyName, str, m_strFilePath);  if (!bRet)  {  throw WindowsException(GetLastError());  }  return bRet;}BOOL MyIni::ReadFileString(const TCHAR* AppName, const TCHAR* KeyName, TCHAR* str, DWORD nSize){BOOL bRet = TRUE;if (!_IsFilePath())throw WindowsException(_T("写Ini需要先设置路径"));if (!GetPrivateProfileString(AppName, KeyName, nullptr, str, nSize, m_strFilePath)){bRet = FALSE;throw WindowsException(GetLastError());}return bRet;}size_t MyIni::ReadFileInt(const TCHAR* AppName, const TCHAR* KeyName){if (!_IsFilePath())throw WindowsException(_T("写Ini需要先设置路径"));size_t size = GetPrivateProfileInt(AppName, KeyName, -1, m_strFilePath);if (size == -1)throw WindowsException(_T("ReadFileInt 读取失败"));return size;}double MyIni::ReadFileDouble(const TCHAR* AppName, const TCHAR* KeyName){TCHAR str[MAX_PATH] = { 0 };ReadFileString(AppName, KeyName, str, MAX_PATH);double ret = _ttof(str);if (fabs(ret) < DBL_EPSILON){throw WindowsException(_T("ReadFileDouble 读取失败"));}return ret;}MyIni::~MyIni(){if (_IsFilePath())delete[]m_strFilePath;}BOOL MyIni::_IsFilePath() const{return m_strFilePath != nullptr ? TRUE : FALSE;}
测试代码   
WCHAR str[MAX_PATH] = { 0 };MyIni ini(_T("test.ini"));ini.WriteFile(_T("ID"), _T("name"), _T("color"));ini.WriteFile(_T("ID"), _T("age"), _T("18"));ini.WriteFile(_T("ID"), _T("deposit"), _T("9.9"));//好穷啊。。。 ini.ReadFileString(L"ID", L"name", str, MAX_PATH);intage = ini.ReadFileInt(L"ID", L"age");doubledeposit = ini.ReadFileDouble(L"ID", L"deposit");

输出 结果  
color  18  9.9

阅读全文
0 0
原创粉丝点击