C++ 读写INI文件

.h

#include <afxcoll.h>

class CIniMgr
{
public:
	// 设定INI文件的名字,要完全路径名
	void setINIFileName(CString strINIFile);
	// 获取段类所有数据 
	CStringList* getSectionData(CString strSection);
	// 所有段名
	CStringList* getSectionNames();
	// 判断段名是否已经存在
	BOOL sectionExists(CString strSection);
	// 设置键值
	long setKey(CString strValue, CString strKey, CString strSection);
	// 获得键值
	CString getKeyValue(CString strKey,CString strSection);
	// 构造函数
	CIniMgr()
	{
		m_sectionList = new CStringList();
		m_sectionDataList = new CStringList();
	}
	// 带文件名参数的构造函数
	CIniMgr(CString strFile)
	{
		m_strFileName = strFile;
		m_sectionList = new CStringList();
		m_sectionDataList = new CStringList();
	}
	// 析构清空链表释放
	~CIniMgr()
	{
		delete m_sectionList;
		delete m_sectionDataList;
	}
private:
	CStringList *m_sectionDataList;
	CStringList *m_sectionList;
	CString m_strSection;
	long m_lRetValue;
	CString m_strFileName;
};

.c

#include "stdafx.h"
#include "IniMgr.h"
#include <afxcoll.h>

CString CIniMgr::getKeyValue(CString strKey,CString strSection)
{
	char ac_Result[255];
	
	m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,(LPCTSTR)strKey,
		"",ac_Result, 255, (LPCTSTR)m_strFileName); 
	
	CString strResult(ac_Result);
	return strResult;
}

long CIniMgr::setKey(CString strValue, CString strKey, CString strSection)
{
	m_lRetValue = WritePrivateProfileString (strSection, strKey, 
		strValue, m_strFileName);
	
	return m_lRetValue;
}

BOOL CIniMgr::sectionExists(CString strSection)
{
	char ac_Result[100]; 
	CString csAux;
	
	m_lRetValue = GetPrivateProfileString((LPCTSTR)strSection,NULL,
		"",ac_Result, 90, (LPCTSTR)m_strFileName);
	
	return (m_lRetValue > 0);
}


CStringList* CIniMgr::getSectionNames() 
{
	char ac_Result[2000];
	m_sectionList->RemoveAll();
	
	m_lRetValue = GetPrivateProfileSectionNames(ac_Result,2000,(LPCTSTR)m_strFileName);
	
	CString strSectionName;
	for(int i=0; i<m_lRetValue; i++)
	{
		if(ac_Result[i] != '\0') {
			strSectionName = strSectionName + ac_Result[i];
		} else {
			if(strSectionName != "") {
				m_sectionList->InsertAfter(m_sectionList->GetTailPosition(),strSectionName);
			}
			strSectionName = "";
		}
	}
	
	return m_sectionList;
}


CStringList* CIniMgr::getSectionData(CString strSection)  
{
	char ac_Result[2000]; 
	m_sectionDataList->RemoveAll();
	m_lRetValue = GetPrivateProfileSection((LPCTSTR)strSection, ac_Result, 2000, (LPCTSTR)m_strFileName);
	
	CString strSectionData;
	for(int i=0; i<m_lRetValue; i++)
	{
		if(ac_Result[i] != '=') { // 以前为if(ac_Result[i] != '\0') {
			strSectionData = strSectionData + ac_Result[i];
		} else {
			if(strSectionData != "") {
				m_sectionDataList->InsertAfter(m_sectionDataList->GetTailPosition(),strSectionData);
			}
			strSectionData = "";
		}
	}
	
	return m_sectionDataList;
}

void CIniMgr::setINIFileName(CString strINIFile)
{
	m_strFileName = strINIFile;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值