您的位置:首页 > 其它

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: