ini文件读写封装类
2017-04-07 10:09
393 查看
#ifndef _PROFILE_H_
#define _PROFILE_H_
/**
* 读写INI文件的类
*/
class CProfile
{
public:
CProfile();
~CProfile();
/// 设置INI文件名
VOID SetProfile( LPCTSTR szIniName );
public:
/// 保存INI文件名的字符串
TCHAR m_szIniName[MAX_PATH];
/// 取INI文件中的FLOAT类型的键值
float GetFloat( LPCTSTR szSection, LPCTSTR szName, float fDefault );
/// 取INI文件中的整型的键值
long GetLong( LPCTSTR szSection, LPCTSTR szName, long lDefault );
/// 取INI文件中的无符号16进制整型类型的键值
DWORD GetHex( LPCTSTR szSection, LPCTSTR szName, DWORD dwDefault );
/// 取INI文件中的字符串类型的键值
CString GetString( LPCTSTR strSection, LPCTSTR szName, LPCTSTR szDefault );
/// 写INI文件中的字符串类型的键值
BOOL SetString( LPCTSTR strSection, LPCTSTR szName, LPCTSTR szValue );
/// 写INI文件中的整型的键值
BOOL SetLong( LPCTSTR strSection, LPCTSTR szName, long lValue );
/// 写INI文件中的整型的键值
BOOL SeFloat( LPCTSTR strSection, LPCTSTR szName, float fValue );
/// 写INI文件中的FLOAT类型的键值
BOOL SeHex( LPCTSTR strSection, LPCTSTR szName, DWORD dwValue );
};
#endif
//-----------------------------------------------------------------------------
// File: Profile.cpp
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "profile.h"
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CProfile::CProfile()
{
GetModuleFileName(NULL, m_szIniName, _MAX_PATH);
int iLen = lstrlen(m_szIniName);
m_szIniName[iLen - 3] = 'i';
m_szIniName[iLen - 2] = 'n';
m_szIniName[iLen - 1] = 'i';
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CProfile::~CProfile()
{
}
//-----------------------------------------------------------------------------
// Name:
// Desc: this function reads our profile into member variables of the profile object.
//-----------------------------------------------------------------------------
VOID CProfile::SetProfile( LPCTSTR szIniName )
{
lstrcpy( m_szIniName, szIniName );
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
DWORD CProfile::GetHex( LPCTSTR strSection, LPCTSTR szName, DWORD dwDefault )
{
TCHAR szBuffer[MAX_PATH];
DWORD dwResult = 0;
GetPrivateProfileString( strSection, szName, TEXT(""),
szBuffer, sizeof(szBuffer), m_szIniName );
if( szBuffer[0] == '\0' )
return dwDefault;
if ( _stscanf( szBuffer, TEXT("0x%x"), &dwResult ) != 1 )
{
dwResult = 0;
}
return dwResult;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
float CProfile::GetFloat( LPCTSTR strSection, LPCTSTR szName, float fDefault )
{
TCHAR szBuffer[MAX_PATH];
GetPrivateProfileString( strSection, szName, TEXT(""),
szBuffer, sizeof(szBuffer), m_szIniName );
if( szBuffer[0] == '\0' )
return fDefault;
return (float)_tcstod(szBuffer,TEXT('\0'));
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
long CProfile::GetLong( LPCTSTR strSection, LPCTSTR szName, long lDefault )
{
TCHAR szBuffer[MAX_PATH];
GetPrivateProfileString( strSection, szName, TEXT(""),
szBuffer, sizeof(szBuffer), m_szIniName );
if( szBuffer[0] == '\0' )
return lDefault;
return _tcstol(szBuffer,TEXT('\0'),10);
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CString CProfile::GetString( LPCTSTR strSection, LPCTSTR szName, LPCTSTR szDefault )
{
TCHAR szBuffer[MAX_PATH];
CString szValue = _T("");
GetPrivateProfileString( strSection, szName, szDefault,
szBuffer, MAX_PATH, m_szIniName );
int iLen = lstrlen(szBuffer);
if ( iLen > 0 )
szValue = szBuffer;
return szValue;
}
BOOL CProfile::SetString( LPCTSTR strSection, LPCTSTR szName, LPCTSTR szValue )
{
return WritePrivateProfileString(strSection, szName, szValue, m_szIniName);
}
BOOL CProfile::SetLong( LPCTSTR strSection, LPCTSTR szName, long lValue )
{
CString strValue;
strValue.Format( _T("%d"), lValue );
return WritePrivateProfileString(strSection, szName, strValue, m_szIniName);
}
BOOL CProfile::SeFloat( LPCTSTR strSection, LPCTSTR szName, float fValue )
{
CString strValue;
strValue.Format( _T("%f"), fValue );
return WritePrivateProfileString(strSection, szName, strValue, m_szIniName);
}
BOOL CProfile::SeHex( LPCTSTR strSection, LPCTSTR szName, DWORD dwValue )
{
CString strValue;
strValue.Format( _T("%x"), dwValue );
return WritePrivateProfileString(strSection, szName, strValue, m_szIniName);
}
#define _PROFILE_H_
/**
* 读写INI文件的类
*/
class CProfile
{
public:
CProfile();
~CProfile();
/// 设置INI文件名
VOID SetProfile( LPCTSTR szIniName );
public:
/// 保存INI文件名的字符串
TCHAR m_szIniName[MAX_PATH];
/// 取INI文件中的FLOAT类型的键值
float GetFloat( LPCTSTR szSection, LPCTSTR szName, float fDefault );
/// 取INI文件中的整型的键值
long GetLong( LPCTSTR szSection, LPCTSTR szName, long lDefault );
/// 取INI文件中的无符号16进制整型类型的键值
DWORD GetHex( LPCTSTR szSection, LPCTSTR szName, DWORD dwDefault );
/// 取INI文件中的字符串类型的键值
CString GetString( LPCTSTR strSection, LPCTSTR szName, LPCTSTR szDefault );
/// 写INI文件中的字符串类型的键值
BOOL SetString( LPCTSTR strSection, LPCTSTR szName, LPCTSTR szValue );
/// 写INI文件中的整型的键值
BOOL SetLong( LPCTSTR strSection, LPCTSTR szName, long lValue );
/// 写INI文件中的整型的键值
BOOL SeFloat( LPCTSTR strSection, LPCTSTR szName, float fValue );
/// 写INI文件中的FLOAT类型的键值
BOOL SeHex( LPCTSTR strSection, LPCTSTR szName, DWORD dwValue );
};
#endif
//-----------------------------------------------------------------------------
// File: Profile.cpp
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "profile.h"
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CProfile::CProfile()
{
GetModuleFileName(NULL, m_szIniName, _MAX_PATH);
int iLen = lstrlen(m_szIniName);
m_szIniName[iLen - 3] = 'i';
m_szIniName[iLen - 2] = 'n';
m_szIniName[iLen - 1] = 'i';
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CProfile::~CProfile()
{
}
//-----------------------------------------------------------------------------
// Name:
// Desc: this function reads our profile into member variables of the profile object.
//-----------------------------------------------------------------------------
VOID CProfile::SetProfile( LPCTSTR szIniName )
{
lstrcpy( m_szIniName, szIniName );
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
DWORD CProfile::GetHex( LPCTSTR strSection, LPCTSTR szName, DWORD dwDefault )
{
TCHAR szBuffer[MAX_PATH];
DWORD dwResult = 0;
GetPrivateProfileString( strSection, szName, TEXT(""),
szBuffer, sizeof(szBuffer), m_szIniName );
if( szBuffer[0] == '\0' )
return dwDefault;
if ( _stscanf( szBuffer, TEXT("0x%x"), &dwResult ) != 1 )
{
dwResult = 0;
}
return dwResult;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
float CProfile::GetFloat( LPCTSTR strSection, LPCTSTR szName, float fDefault )
{
TCHAR szBuffer[MAX_PATH];
GetPrivateProfileString( strSection, szName, TEXT(""),
szBuffer, sizeof(szBuffer), m_szIniName );
if( szBuffer[0] == '\0' )
return fDefault;
return (float)_tcstod(szBuffer,TEXT('\0'));
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
long CProfile::GetLong( LPCTSTR strSection, LPCTSTR szName, long lDefault )
{
TCHAR szBuffer[MAX_PATH];
GetPrivateProfileString( strSection, szName, TEXT(""),
szBuffer, sizeof(szBuffer), m_szIniName );
if( szBuffer[0] == '\0' )
return lDefault;
return _tcstol(szBuffer,TEXT('\0'),10);
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CString CProfile::GetString( LPCTSTR strSection, LPCTSTR szName, LPCTSTR szDefault )
{
TCHAR szBuffer[MAX_PATH];
CString szValue = _T("");
GetPrivateProfileString( strSection, szName, szDefault,
szBuffer, MAX_PATH, m_szIniName );
int iLen = lstrlen(szBuffer);
if ( iLen > 0 )
szValue = szBuffer;
return szValue;
}
BOOL CProfile::SetString( LPCTSTR strSection, LPCTSTR szName, LPCTSTR szValue )
{
return WritePrivateProfileString(strSection, szName, szValue, m_szIniName);
}
BOOL CProfile::SetLong( LPCTSTR strSection, LPCTSTR szName, long lValue )
{
CString strValue;
strValue.Format( _T("%d"), lValue );
return WritePrivateProfileString(strSection, szName, strValue, m_szIniName);
}
BOOL CProfile::SeFloat( LPCTSTR strSection, LPCTSTR szName, float fValue )
{
CString strValue;
strValue.Format( _T("%f"), fValue );
return WritePrivateProfileString(strSection, szName, strValue, m_szIniName);
}
BOOL CProfile::SeHex( LPCTSTR strSection, LPCTSTR szName, DWORD dwValue )
{
CString strValue;
strValue.Format( _T("%x"), dwValue );
return WritePrivateProfileString(strSection, szName, strValue, m_szIniName);
}
相关文章推荐
- 封装 INI 文件读写函数
- 关于Ini 文件的读写----一份封装的类
- 封装 INI 文件读写函数
- 一个跨平台c++封装的.ini文件读写类
- ini文件纯C++读写代码
- ini文件纯C++读写代码
- 如何在 VFP 中读写 INI 文件
- INI文件读写
- 文件读写封装
- 用java读写ini配置文件
- ini文件纯C++读写代码
- 用API读写INI配置文件
- 用java读写ini配置文件(转)
- 用BCB读写文本文件及ini配置文件
- WinAPI: WritePrivateProfileString、GetPrivateProfileString - 简单读写 Ini 文件
- ini文件纯C++读写代码
- 用API读写INI配置文件
- 读写INI配置文件的Java类
- 用java读写ini配置文件的原因以及实现 示例
- 跨平台INI文件读写API(C++版本)