C++ 读取INI文件
2013-10-16 13:51
267 查看
我们通常需要用到配置文件,Windows支持读取INI文件。
总结为一个类:
如果读取的INI文件中有中文字符。那么此文件需要与编程环境的编码一样。
比如,采用UNICODE编程,那么INI文件需要保存为UNICODE格式,否则中文字符读出来为乱码。
总结为一个类:
class CMyINI { public: CMyINI(CString strIniName); ~CMyINI(void); void GetFromIniFileValueINT(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT *pIntValue); void GetFromIniFileValueINT(LPCTSTR lpAppName, LPCTSTR lpKeyName, DWORD *pIntValue); void GetFromIniFileValueCString(LPCTSTR lpAppName, LPCTSTR lpKeyName, CString *pstrValueString); void SetIntoIniFileValueDWORD(LPCTSTR lpAppName, LPCTSTR lpKeyName, DWORD dwValue); void SetIntoIniFileValueCString(LPCTSTR lpAppName, LPCTSTR lpKeyName, CString strValueString); void AddIniFileValueDWORD(LPCTSTR lpAppName, LPCTSTR lpKeyName, DWORD num = 1); private: CString m_strIniName; };
CMyINI::CMyINI(CString strIniName) { m_strIniName = strIniName; } CMyINI::~CMyINI(void) { } /************************************************************************/ // 函数名称: GetFromIniFileValueINT // 作 者:豆浆 // 日 期:2013/02/25 // 返 回 值: // 参 数: // 函数说明:读取INT /************************************************************************/ void CMyINI::GetFromIniFileValueINT(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT *pIntValue) { *pIntValue = GetPrivateProfileInt(lpAppName, lpKeyName, 0, m_strIniName); } /************************************************************************/ // 函数名称: GetFromIniFileValueINT // 作 者:豆浆 // 日 期:2013/02/25 // 返 回 值: // 参 数: // 函数说明:读取DWORD void CMyINI::GetFromIniFileValueINT(LPCTSTR lpAppName, LPCTSTR lpKeyName, DWORD *pIntValue) { *pIntValue = GetPrivateProfileInt(lpAppName, lpKeyName, 0, m_strIniName); } /************************************************************************/ // 函数名称: GetFromIniFileValueCString // 作 者:豆浆 // 日 期:2013/02/25 // 返 回 值: // 参 数: // 函数说明: /************************************************************************/ void CMyINI::GetFromIniFileValueCString(LPCTSTR lpAppName, LPCTSTR lpKeyName, CString *pstrValueString) { WCHAR wcrValue[MAX_PATH] = {0}; GetPrivateProfileString(lpAppName, lpKeyName, _T(""), wcrValue, MAX_PATH, m_strIniName); pstrValueString->Format(_T("%s"), wcrValue); } /************************************************************************/ // 函数名称: SetIntoIniFileValueDWORD // 作 者:豆浆 // 日 期:2013/02/25 // 返 回 值: // 参 数: // 函数说明: /************************************************************************/ void CMyINI::SetIntoIniFileValueDWORD(LPCTSTR lpAppName, LPCTSTR lpKeyName, DWORD dwValue) { CString strValueString; strValueString.Format(_T("%d"), dwValue); WritePrivateProfileString(lpAppName, lpKeyName, strValueString, m_strIniName); } /************************************************************************/ // 函数名称: SetIntoIniFileValueCString // 作 者:豆浆 // 日 期:2013/02/25 // 返 回 值: // 参 数: // 函数说明: /************************************************************************/ void CMyINI::SetIntoIniFileValueCString(LPCTSTR lpAppName, LPCTSTR lpKeyName, CString strValueString) { WritePrivateProfileString(lpAppName, lpKeyName, strValueString, m_strIniName); } void CMyINI::AddIniFileValueDWORD(LPCTSTR lpAppName, LPCTSTR lpKeyName, DWORD num) { INT ntmp = 0; GetFromIniFileValueINT(lpAppName, lpKeyName, &ntmp); ntmp += num; SetIntoIniFileValueDWORD(lpAppName, lpKeyName, ntmp); }
如果读取的INI文件中有中文字符。那么此文件需要与编程环境的编码一样。
比如,采用UNICODE编程,那么INI文件需要保存为UNICODE格式,否则中文字符读出来为乱码。
相关文章推荐
- C++ 中使用boost::property_tree读取解析ini文件
- C++读取INI文件
- C++读取ini文件的类
- C++ 中使用boost::property_tree读取解析ini文件
- CPP-基础:c++读取ini文件
- C#读取ini文件方法,实质是使用C++的库
- C++读取INI文件
- c++ 读取ini文件
- GetPrivateProfileString相关--C++从.ini文件读取配置信息
- C++ ini文件的读取与写入
- C++ 读取INI文件
- C++读取INI文件
- c++ 读取csv文件
- C++读取一整行字符串
- C++ 操作64位系统,默认读取Wow6432Node子键的解决方法。
- C/C++——打开文件读取数据的各种方式
- Debug日志:C++读取任意磁盘、任意路径下的文件
- C++中Txt文件读取和写入
- C++读取系统当前时间
- c++ 字符输入读取