c++ windows 使用 i配置文件
来源:互联网 发布:java界面编程实例 编辑:程序博客网 时间:2024/06/14 01:37
INI
(文件扩展名)
INI文件格式是某些平台或软件上的配置文件的非正式标准,以节(section)和键(key)构成,常用于微软Windows操作系统中。这种配置文件的文件扩展名多为INI,故名。INI是英文“初始化”(initialization)的缩写。正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置。微软提供了三个函数 来使用 写入 读取 string 读取 intWritePrivateProfileString 写入GetPrivateProfileString 读取 stringGetPrivateProfileInt 读取 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
- c++ windows 使用 i配置文件
- C语言:使用配置文件
- Windows系统使用INI配置文件
- c程序使用uci配置文件
- Windows 2000是使用U n i c o d e从头进行开发的
- windows Gvim c/c++实用配置文件
- windows C:\WINDOWS\system32\drivers\etc\hosts网络配置文件
- Java I/O Properties的使用 存取配置文件
- linux c 使用libxml2读取配置文件
- C/C++ 中使用lua作为配置文件
- Windows系统让apache使用其他名称的配置文件修改配置文件名称指定自己的配置文件
- c语言中i++与++i的使用技巧
- 《Windows Via C/C++》学习之同步设备I/O与异步设备I/O
- 《Windows via C/C++》学习笔记 (七) 设备I/O之“I/O请求完成”
- 《Windows via C/C++》学习笔记 —— 设备I/O之“I/O完成端口”
- C中&与I的使用
- C中的 i++,++i
- C语言linux I/O函数集与windows I/O 函数集
- 分析Java AtomicInteger
- 如何给 eclipse 设置快捷键
- Fail-soft alpha-beta
- 记录下安装win7+centos双系统的经验,主要是转载
- 慕课网实战项目《WebAPP书城整站开发》笔记一:底部边栏样式实现
- c++ windows 使用 i配置文件
- Maven项目错误解决
- 慕课网实战项目《WebAPP书城整站开发》笔记二:基本的JS代码结构
- ACM经验总结版
- 慕课网实战项目《WebAPP书城整站开发》笔记三:触屏唤出上下边栏交互开发和优化
- ACM队伍训练题目池
- server2012/win8 卸载.net framework 4.5后 无法进入系统桌面故障解决
- 1020. Tree Traversals (25)
- WE,加油