您的位置:首页 > 编程语言 > C语言/C++

C++ 读取INI文件

2013-10-16 13:51 267 查看
我们通常需要用到配置文件,Windows支持读取INI文件。

总结为一个类:

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格式,否则中文字符读出来为乱码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: