-
-
INI
(文件扩展名)
INI文件格式是某些平台或软件上的配置文件的非正式标准,以节(section)和键(key)构成,常用于微软Windows操作系统中。这种配置文件的文件扩展名多为INI,故名。INI是英文“初始化”(initialization)的缩写。正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置。微软提供了三个函数 来使用 写入 读取 string 读取 intWritePrivateProfileString 写入
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);
int age = ini.ReadFileInt(L"ID", L"age");
double deposit = ini.ReadFileDouble(L"ID", L"deposit");
输出 结果
color 18 9.9