配置文件:INI的简单封装

37 篇文章 4 订阅
6 篇文章 0 订阅

一、前言

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值