Ini配置文件

头文件:

#ifndef INIFILE_H
#define INIFILE_H

#include "string"
#include <iostream>
using namespace std;
class IniFile
{
public:
	IniFile(string m_szFileName);
	
	void writeInteger(string szSection, string szKey, int iValue);
	void writeString(string szSection, string szKey, string szValue);

	int readInteger(string szSection, string szKey, int iDefaultValue);
	string readString(string szSection, string szKey, string szDefaultValue);

	void removeSection(string szSection);
	void removeKey(string szSection, string szKey);

private:
	string m_szFileName;
};

#endif //INIFILE_H

CPP:

#include "ini_file.h"
#include <Windows.h>
#include "DbgPrint.h"
using namespace std;

#define MAX_LEN 255

IniFile::IniFile(string szFileName)
{
	//C盘无权限创建文件
	std::string szAppdata = getenv("appdata");
	m_szFileName = szAppdata+"\\zz\\"+szFileName;
}

void IniFile::writeInteger(string szSection, string szKey, int iValue)
{
	char szValue[MAX_LEN] = {0};
	_ltoa_s(iValue,szValue,10);
	if (!WritePrivateProfileStringA(szSection.c_str(), szKey.c_str(), 
		szValue, m_szFileName.c_str()))
	{
		DbgPrint("[IniFile]writeInteger failed,err = %d",GetLastError());
	}
}

void IniFile::writeString(string szSection, string szKey, string szValue)
{
	if (!WritePrivateProfileStringA(szSection.c_str(), szKey.c_str(), 
		szValue.c_str(), m_szFileName.c_str()))
	{
		DbgPrint("[IniFile]writeString failed,err = %d",GetLastError());
	}
	DbgPrint("[IniFile]writeString ok,err = %d",GetLastError());
}

int IniFile::readInteger(string szSection, string szKey, int iDefaultValue)
{
	int iResult = GetPrivateProfileIntA(szSection.c_str(),  szKey.c_str(),
		iDefaultValue, m_szFileName.c_str());
	return iResult;
}

string IniFile::readString(string szSection, string szKey, string szDefaultValue)
{
	char szResult[MAX_LEN];
	memset(szResult, 0x00, MAX_LEN);
	GetPrivateProfileStringA(szSection.c_str(),  szKey.c_str(), 
		szDefaultValue.c_str(), szResult, MAX_LEN, m_szFileName.c_str());
	return szResult;
}

void IniFile::removeSection(string szSection)
{
	if (!WritePrivateProfileStringA(szSection.c_str(),NULL,NULL,m_szFileName.c_str()))
	{
		DbgPrint("[IniFile]removeSection failed,err = %d",GetLastError());
	}
}

void IniFile::removeKey(string szSection, string szKey)
{
	if (!WritePrivateProfileStringA(szSection.c_str(),szKey.c_str(),NULL,m_szFileName.c_str()))
	{
		DbgPrint("[IniFile]removeKey failed,err = %d",GetLastError());
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值