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