VC的Ini文件读写类

Delphi用惯了,今天写族谱管理时发现VC居然没有ini文件操作的类(可能是我无知),只好自己动手。仿照Delphi的TIniFile写的,功能如下:

bool SectionExists(CString Section);
CString ReadString(CString Section, CString Ident, CString Default);
void WriteString(CString Section, CString Ident, CString Value);
int ReadInteger(CString Section, CString Ident, int Default);
void WriteInteger(CString Section, CString Ident, int Value);
void ReadSection(CString Section, CStringArray &Strings);
void ReadSections(CStringArray &Strings);
void ReadSectionValues(CString Section, CStringArray &Strings);
void EraseSection(CString Section);

 

头文件

// IniFile.h: interface for the CIniFiles class.
//
//

#if !defined(AFX_INIFILES_H__F09B5546_796F_44B3_95A5_E2F7D053732C__INCLUDED_)
#define AFX_INIFILES_H__F09B5546_796F_44B3_95A5_E2F7D053732C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define MAX_BUFFER_SIZE 2048;

class CIniFileException
{
public:
CIniFileException() {};
~CIniFileException() {};
};

class CIniFile 
{
public:
CIniFile(CString FileName);
virtual ~CIniFile();
bool SectionExists(CString Section);
CString ReadString(CString Section, CString Ident, CString Default);
void WriteString(CString Section, CString Ident, CString Value);
int ReadInteger(CString Section, CString Ident, int Default);
void WriteInteger(CString Section, CString Ident, int Value);
void ReadSection(CString Section, CStringArray &Strings);
void ReadSections(CStringArray &Strings);
void ReadSectionValues(CString Section, CStringArray &Strings);
void EraseSection(CString Section);

protected:
CString m_strFileName;
};

#endif // !defined(AFX_INIFILES_H__F09B5546_796F_44B3_95A5_E2F7D053732C__INCLUDED_)

 

源文件

// IniFile.cpp: implementation of the CIniFiles class.
//
//

#include "stdafx.h"
#include "IniFile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//
// Construction/Destruction
//

CIniFile::CIniFile(CString FileName)
{
	m_strFileName = FileName;
}

CIniFile::~CIniFile()
{
}

bool CIniFile::SectionExists(CString Section)
{
	CStringArray Strings;
	ReadSection(Section, Strings);
	return Strings.GetSize()>0;
}

CString CIniFile::ReadString(CString Section, CString Ident, CString Default)
{
	
	int nSize = 0, nLen = nSize-2;
	char *lpszReturnBuffer = 0;
	while(nLen==nSize-2)
	{
		nSize+=MAX_BUFFER_SIZE;
		if(lpszReturnBuffer) delete lpszReturnBuffer;
		lpszReturnBuffer = new char[nSize];
		nLen = GetPrivateProfileString(Section,Ident,Default,lpszReturnBuffer,//如果返回nSize-2
			nSize,m_strFileName);	//则表示缓冲区长度不足,递增MAX_BUFFER_SIZE
	}
	return lpszReturnBuffer;
}

void CIniFile::WriteString(CString Section, CString Ident, CString Value)
{
	if(WritePrivateProfileString(Section,Ident,Value,m_strFileName)==0)
		throw CIniFileException();
}

int CIniFile::ReadInteger(CString Section, CString Ident, int Default)
{
	CString strInt = ReadString(Section,Ident,"");
	if (strInt.IsEmpty())
	{
		return Default;
	}
	else
	{
		return atoi(strInt);
	}
}

void CIniFile::WriteInteger(CString Section, CString Ident, int Value)
{
	char buffer[20];
	itoa(Value,buffer,10);
	WriteString(Section,Ident,buffer);
}

void CIniFile::ReadSection(CString Section, CStringArray &Strings)
{
	Strings.RemoveAll();//清空字符串数组
	int nSize = 0, nLen = nSize-2;
	char *lpszReturnBuffer = 0;
	while(nLen==nSize-2)
	{
		nSize+=MAX_BUFFER_SIZE;
		if(lpszReturnBuffer) delete lpszReturnBuffer;
		lpszReturnBuffer = new char[nSize];
		nLen = GetPrivateProfileString(Section,NULL,NULL,lpszReturnBuffer,//如果返回nSize-2
			nSize,m_strFileName);	//则表示缓冲区长度不足,递增MAX_BUFFER_SIZE
	}
	char *pName = new char[MAX_PATH];
	char *pStart, *pEnd;
	pStart = lpszReturnBuffer;
	pEnd =0;
	while(pStart!=pEnd)
	{
		pEnd = strchr(pStart,0);
		nLen = pEnd-pStart;
		if(nLen==0) break;
		strncpy(pName,pStart,nLen);
		pName[nLen] = 0;
		Strings.Add(pName);
		pStart = pEnd+1;
	}	
	delete lpszReturnBuffer;
	delete pName;
}

void CIniFile::ReadSections(CStringArray &Strings)
{
	Strings.RemoveAll();//清空字符串数组
	int nSize = 0, nLen = nSize-2;
	char *lpszReturnBuffer = 0;
	while(nLen==nSize-2)
	{
		nSize+=MAX_BUFFER_SIZE;
		if(lpszReturnBuffer) delete lpszReturnBuffer;
		lpszReturnBuffer = new char[nSize];
		nLen = GetPrivateProfileSectionNames(lpszReturnBuffer,nSize,//如果返回nSize-2则表示
			m_strFileName);	//缓冲区长度不足,递增MAX_BUFFER_SIZE
	}
	char *pName = new char[MAX_PATH];
	char *pStart, *pEnd;
	pStart = lpszReturnBuffer;
	pEnd =0;
	while(pStart!=pEnd)
	{
		pEnd = strchr(pStart,0);
		nLen = pEnd-pStart;
		if(nLen==0) break;
		strncpy(pName,pStart,nLen);
		pName[nLen] = 0;
		Strings.Add(pName);
		pStart = pEnd+1;
	}
	delete lpszReturnBuffer;
	delete pName;
}

void CIniFile::EraseSection(CString Section)
{
	if(WritePrivateProfileString(Section,NULL,NULL,m_strFileName)==0)
		throw CIniFileException();
}
转自:http://ilue.bokee.com/2164221.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值