1.头文件
#ifndef INIRW_H
#define INIRW_H
#pragma once
#include <atlstr.h>
#define DEFAULT_INI_PATH _T(".\\setup.ini")
class CIniRW
{
public:
CIniRW();
CIniRW(LPCTSTR path);
~CIniRW();
LPCTSTR SetIniFilePath(LPCTSTR path);
LPCTSTR ReadString(LPCTSTR sectionName, LPCTSTR keyName, LPCTSTR defaultValue=NULL);
int ReadInt(LPCTSTR sectionName, LPCTSTR keyName, const int defaultValue=0);
int ReadHex(LPCTSTR sectionName, LPCTSTR keyName, const int defaultValue=0x0);
BOOL ReadBool(LPCTSTR sectionName, LPCTSTR keyName, const BOOL defaultValue=FALSE);
double ReadDouble(LPCTSTR sectionName, LPCTSTR keyName, const double defaultValue=0.0);
float ReadFloat(LPCTSTR sectionName, LPCTSTR keyName, const float defaultValue=0.0);
BOOL WriteString(LPCTSTR sectionName, LPCTSTR keyName, LPCTSTR string);
BOOL WriteInt(LPCTSTR sectionName, LPCTSTR keyName, const int data);
BOOL WriteHex(LPCTSTR sectionName, LPCTSTR keyName, const int data, const int dataSize=2);
BOOL WriteBool(LPCTSTR sectionName, LPCTSTR keyName, const BOOL data);
BOOL WriteDouble(LPCTSTR sectionName, LPCTSTR keyName, const double data);
BOOL WriteFloat(LPCTSTR sectionName, LPCTSTR keyName, const float data);
BOOL DeleteSection(LPCTSTR sectionName);
BOOL DeleteKey(LPCTSTR sectionName, LPCTSTR keyName);
protected:
void Init(void);
private:
CString m_filePath;
};
#endif /* INIRW_H */
2.源文件
#include "StdAfx.h"
#include "IniRW.h"
CIniRW::CIniRW()
{
Init();
}
CIniRW::CIniRW(LPCTSTR path)
{
Init();
m_filePath = path;
}
CIniRW::~CIniRW()
{
}
void CIniRW::Init(void)
{
m_filePath = DEFAULT_INI_PATH;
}
LPCTSTR CIniRW::SetIniFilePath(LPCTSTR path)
{
static CString oldPath = m_filePath;
m_filePath = path;
return oldPath;
}
LPCTSTR CIniRW::ReadString(LPCTSTR sectionName, LPCTSTR keyName, LPCTSTR defaultValue/*=NULL*/)
{
static CString temp;
GetPrivateProfileString(sectionName, keyName, defaultValue, temp.GetBuffer(MAX_PATH), MAX_PATH, m_filePath);
temp.ReleaseBuffer();
return temp;
}
int CIniRW::ReadInt(LPCTSTR sectionName, LPCTSTR keyName, const int defaultValue/*=0*/)
{
return GetPrivateProfileInt(sectionName, keyName, defaultValue, m_filePath);
}
int CIniRW::ReadHex(LPCTSTR sectionName, LPCTSTR keyName, const int defaultValue/*=0x0*/)
{
return ReadInt(sectionName, keyName, defaultValue);
}
BOOL CIniRW::ReadBool(LPCTSTR sectionName, LPCTSTR keyName, const BOOL defaultValue/*=FALSE*/)
{
if (0 == ReadInt(sectionName, keyName, defaultValue))
{
return FALSE;
}
else
{
return TRUE;
}
}
double CIniRW::ReadDouble(LPCTSTR sectionName, LPCTSTR keyName, const double defaultValue/*=0.0*/)
{
CString temp;
temp.Format(_T("%.lf"), defaultValue);
#if _MSC_VER <= 1200 // for VC6
return atof(ReadString(sectionName, keyName, temp));
#else
return _ttof(ReadString(sectionName, keyName, temp));
#endif
}
float CIniRW::ReadFloat(LPCTSTR sectionName, LPCTSTR keyName, const float defaultValue/*=0.0*/)
{
CString temp;
temp.Format(_T("%.6lf"), defaultValue);
#if _MSC_VER <= 1200 // for VC6
return (float)atof(ReadString(sectionName, keyName, temp));
#else
return (float)_ttof(ReadString(sectionName, keyName, temp));
#endif
}
BOOL CIniRW::WriteString(LPCTSTR sectionName, LPCTSTR keyName, LPCTSTR string)
{
return WritePrivateProfileString(sectionName, keyName, string, m_filePath);
}
BOOL CIniRW::WriteInt(LPCTSTR sectionName, LPCTSTR keyName, const int data)
{
CString temp;
temp.Format(_T("%d"), data);
return WriteString(sectionName, keyName, temp);
}
BOOL CIniRW::WriteHex(LPCTSTR sectionName, LPCTSTR keyName, const int data, const int dataSize/*=2*/)
{
CString temp;
switch (dataSize)
{
case 1:
temp.Format(_T("0x%02x"),data);
break;
case 2:
temp.Format(_T("0x%04x"),data);
break;
case 4:
temp.Format(_T("0x%08x"),data);
break;
default:
temp.Format(_T("0x%x"),data);
break;
}
return WriteString(sectionName, keyName, temp);
}
BOOL CIniRW::WriteBool(LPCTSTR sectionName, LPCTSTR keyName, const BOOL data)
{
return WriteInt(sectionName, keyName, data);
}
BOOL CIniRW::WriteDouble(LPCTSTR sectionName, LPCTSTR keyName, const double data)
{
CString temp;
temp.Format(_T("%.8lf"), data);
return WriteString(sectionName, keyName, temp);
}
BOOL CIniRW::WriteFloat(LPCTSTR sectionName, LPCTSTR keyName, const float data)
{
CString temp;
temp.Format(_T("%.6lf"), data);
return WriteString(sectionName, keyName, temp);
}
BOOL CIniRW::DeleteSection(LPCTSTR sectionName)
{
return WriteString(sectionName, NULL, NULL);
}
BOOL CIniRW::DeleteKey(LPCTSTR sectionName, LPCTSTR keyName)
{
return WriteString(sectionName, keyName, NULL);
}
C++ INI文件读写类实现
本文介绍了一个用于读写INI文件的C++类实现,包括设置路径、读取字符串、整数、十六进制数、布尔值、双精度浮点数和单精度浮点数,以及写入这些数据类型的功能。

被折叠的 条评论
为什么被折叠?



