一、前言
1.1 什么是INI
.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置。
其有三个相关的API,如下,关于他们的使用在MFC读写ini配置文件之中就有说明,不再赘述,直接来对其做一层简单的封装。
WritePrivateProfileString
GetPrivateProfileString
GetPrivateProfileInt
二、封装
2.1 .h
#pragma once
#include <afx.h>
#include <string>
using namespace std;
class OperateIniFile
{
public:
OperateIniFile();
OperateIniFile(CString path);
~OperateIniFile();
bool SetPath(CString filePath);
bool Read(CString Field, CString KeyName, int& result);
bool Read(CString Field, CString KeyName, double& result);
bool Read(CString Field, CString KeyName, CString& result);
bool Read(CString Field, CString KeyName, string& result);
bool Write(CString Field, CString KeyName, int data);
bool Write(CString Field, CString KeyName, double data);
bool Write(CString Field, CString KeyName, CString data);
bool Write(CString Field, CString KeyName, string& data);
private:
CString m_Path;
};
2.2 .cpp
#include "OperateIniFile.h"
OperateIniFile::OperateIniFile()
{
m_Path.Empty();
}
OperateIniFile::OperateIniFile(CString path)
{
if (path.IsEmpty()) {
return;
}
m_Path = path;
}
OperateIniFile::~OperateIniFile()
{
}
bool OperateIniFile::SetPath(CString filePath)
{
m_Path = filePath;
return true;
}
bool OperateIniFile::Read(CString Field, CString KeyName, int& result)
{
if (!m_Path)
{
return false;
}
int ret = GetPrivateProfileInt(Field, KeyName, -100, m_Path);
if (ret == -100){
return false;
}
result = ret;
return true;
}
bool OperateIniFile::Read(CString Field, CString KeyName, CString& result)
{
if (!m_Path)
{
return false;
}
CString strName;
GetPrivateProfileString(Field, KeyName, _T("err"), strName.GetBuffer(MAX_PATH), MAX_PATH, m_Path);
strName.ReleaseBuffer();
if (strName.Compare(_T("err"))== 0) {
return false;
}
result = strName;
return true;
}
bool OperateIniFile::Read(CString Field, CString KeyName, string& result)
{
if (!m_Path)
{
return false;
}
CString strName;
if (!Read(Field, KeyName, strName)) {
return false;
}
result = string(CW2A(strName.GetBuffer()));
strName.ReleaseBuffer();
return true;
}
bool OperateIniFile::Read(CString Field, CString KeyName, double& result)
{
if (!m_Path)
{
return false;
}
CString strName;
if (!Read(Field, KeyName, strName)) {
return false;
}
result = _wtof(strName);
return true;
}
bool OperateIniFile::Write(CString Field, CString KeyName, double data)
{
if (!m_Path)
{
return false;
}
CString keyValue;
keyValue.Format(_T("%lf"), data);
if (WritePrivateProfileString(Field, KeyName, keyValue, m_Path)) {
return true;
}
return false;
}
bool OperateIniFile::Write(CString Field, CString KeyName, CString data)
{
if (!m_Path)
{
return false;
}
if (WritePrivateProfileString(Field, KeyName, data, m_Path)) {
return true;
}
return false;
}
bool OperateIniFile::Write(CString Field, CString KeyName, string& data)
{
if (!m_Path)
{
return false;
}
CString temp = data.c_str();
return Write(Field, KeyName, temp);
}
bool OperateIniFile::Write(CString Field, CString KeyName, int data)
{
if (!m_Path)
{
return false;
}
CString keyValue;
keyValue.Format(_T("%d"), data);
if (WritePrivateProfileString(Field, KeyName, keyValue, m_Path)) {
return true;
}
return false;
}