【MFC】CIniRw类,用于读写后缀为ini的配置文件

.h文件

#pragma once


#include <iostream>
#include <windows.h> 
#include <tchar.h>
#include <stdio.h> 

#include <stdlib.h>
#include <string>

#include "atlstr.h"	

class CIniRw
{
public:
	CIniRw(void);
	~CIniRw(void);

public:
	bool			GetFileStatus(CString strFileName); 
	void			SetFileName(CString strFileName);

public:
	void			WriteInteger(LPCTSTR lpSection, LPCTSTR lpKey, int nValue);
	void			WriteFloat(LPCTSTR lpSection, LPCTSTR lpKey, double dbValue);
	void			WriteBoolean(LPCTSTR lpSection, LPCTSTR lpKey, bool bValue);
	void			WriteString(LPCTSTR lpSection, LPCTSTR lpKey, CString szValue);	


	int				ReadInteger(LPCTSTR lpSection, LPCTSTR lpKey, int iDefaultValue);
	double			ReadFloat(LPCTSTR lpSection, LPCTSTR lpKey, double dbDefaultValue);
	bool			ReadBoolean(LPCTSTR lpSection, LPCTSTR lpKey, bool bolDefaultValue);
	CString			ReadString(LPCTSTR lpSection, LPCTSTR lpKey, CString szDefaultValue);

public:
	CString			m_strFileName ;
};

.cpp文件

#include "StdAfx.h"
#include "IniRw.h"


CIniRw::CIniRw(void)
{
}


CIniRw::~CIniRw(void)
{
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:获取文件状态
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
bool CIniRw::GetFileStatus(CString strFileName)
{
	CString			strStatus;
	TCHAR			sPath[MAX_PATH];
	CString			strPath, strConfigPath;

	GetModuleFileName(NULL, sPath, MAX_PATH);
	strPath = sPath;
	strPath = strPath.Left(strPath.ReverseFind( _T('\\')));
	strConfigPath = strPath + _T("\\") + strFileName; 
	m_strFileName = strConfigPath;

	strStatus = ReadString(_T("FileStatus"), _T("status"), _T("noFile")); 

	if (strStatus == _T("fileExist"))
	{
		return true;
	}
	else
	{
		//WriteString(_T("FileStatus"), _T("status"), _T("fileExist"));     
		return false;
	}
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:设置文件名
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::SetFileName(CString strFileName)
{
	TCHAR			sPath[MAX_PATH];
	CString			strPath, strConfigPath;

	GetModuleFileName(NULL, sPath, MAX_PATH);
	strPath = sPath;
	strPath = strPath.Left(strPath.ReverseFind( _T('\\')));
	strConfigPath = strPath +  _T("\\") + strFileName; 
	m_strFileName = strConfigPath;
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:写整型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::WriteInteger(LPCTSTR lpSection, LPCTSTR lpKey, int nValue)
{
	CString			strValue;

	strValue.Format(_T("%d"), nValue); 
	WritePrivateProfileString(lpSection, lpKey, strValue, m_strFileName); 
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:写浮点型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::WriteFloat(LPCTSTR lpSection, LPCTSTR lpKey, double dbValue)
{
	CString			strValue;
	
	strValue.Format(_T("%.06f"), dbValue); 
	WritePrivateProfileString(lpSection, lpKey, strValue, m_strFileName); 
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:写布尔型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::WriteBoolean(LPCTSTR lpSection, LPCTSTR lpKey, bool bValue)
{
	CString			strValue;

	if (bValue)
		strValue.Format(_T("true"));
	else
		strValue.Format(_T("false"));

	WritePrivateProfileString(lpSection, lpKey, strValue, m_strFileName); 
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:写字符串
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
void CIniRw::WriteString(LPCTSTR lpSection, LPCTSTR lpKey, CString strValue)
{
	WritePrivateProfileString(lpSection, lpKey, strValue, m_strFileName);
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:读整型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
int CIniRw::ReadInteger(LPCTSTR lpSection, LPCTSTR lpKey, int nDefaultValue)
{
	int			nResult;
	
	nResult	= GetPrivateProfileInt(lpSection, lpKey, nDefaultValue, m_strFileName); 
	
	return nResult;
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:写浮点型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
double CIniRw::ReadFloat(LPCTSTR lpSection, LPCTSTR lpKey, double dbDefaultValue)
{
	TCHAR		szResult[255];
	TCHAR		szDefault[255];
	double		dbResult;
	
	GetPrivateProfileString(lpSection, lpKey, szDefault, szResult, 255, m_strFileName); 
	dbResult = atof(szResult);
	
	return dbResult;
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:读布尔型
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
bool CIniRw::ReadBoolean(LPCTSTR lpSection, LPCTSTR lpKey, bool bDefaultValue)
{
	TCHAR		szResult[255];
	TCHAR		szDefault[255];
	CString		strValue ;
	bool		bResult = false;
	
	GetPrivateProfileString(lpSection, lpKey, szDefault, szResult, 255, m_strFileName); 
	strValue = szResult;

	if (strValue == _T("false"))
		bResult = false;
	else if (strValue == _T("true"))
		bResult = true;

	return bResult;
}

//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□// 
//描述:读字符串
//参数:
//□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□//
CString CIniRw::ReadString(LPCTSTR lpSection, LPCTSTR lpKey, CString strDefultValue)
{
	TCHAR		szResult[255];
	CString		strResult; 
	
	memset(szResult, 0x00, 255);
	
	GetPrivateProfileString(lpSection, lpKey, strDefultValue, szResult, 255, m_strFileName); 

	strResult.Format(szResult);

	return strResult;
}

源代码下载:https://download.csdn.net/download/sunriver2000/10321121
 

纯c读写ini配置文件 用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种注释、跨平台换行符支持、带引号字符串处理、无section操作、原格式保持等方面存在问题。 现将本人精心制作的ini读写程序源码奉献给大家,纯c编写,简洁好用。支持windows和linux。 主要特点: 1、支持;和#注释符号,支持行尾注释。 2、支持带引号'或"成对匹配的字符串,提取时自动去引号。引号中可带其它引号或;#注释符。 3、支持无section或空section(名称为空)。 4、支持10、16、8进制数,0x开头为16进制数,0开头为8进制。 5、支持section、key或=号前后带空格。 6、支持\n、\r、\r\n或\n\r换行格式。 7、不区分section、key大小写,但写入时以新串为准,并保持其大小写。 8、新增数据时,若section存在则在该节最后一个有效数据后添加,否则在文件尾部添加。 9、支持指定key所在整行删除,即删除该键值,包括注释。 10、可自动跳过格式错误行,修改时仍然保留。 11、修改时保留原注释:包括整行注释、行尾注释(包括前面空格)。 12、修改时保留原空行。以上三点主要是尽量保留原格式。 不足之处: 1、不支持单key多value(逗号分割),只能一次性提取后自行处理。 2、不支持同名重复section和key。(重复section可视为错误,重复key则可能造成分歧) 3、不能提取所有section或key名称。 使用只需两个文件inirw.h、inirw.c,另有测试程序和工程文件,支持windows和linux。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值