VC的Ini文件读写类
来源:互联网 发布:升基网络怎么样 编辑:程序博客网 时间:2024/05/16 08:24
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_FILEstatic 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-2nSize,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-2nSize,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
- VC的Ini文件读写类
- VC INI文件的读写
- VC读写INI文件
- VC读写INI文件
- VC读写ini文件
- VC读写INI文件
- VC读写INI文件
- VC读写INI文件
- VC读写ini文件
- VC读写INI文件
- VC INI文件读写
- VC读写INI文件
- VC读写INI文件
- VC读写INI文件
- VC读写INI文件
- VC读写INI文件
- VC 读写INI文件
- VC INI文件读写
- 搜狗浏览器的8点个人体会
- android 的mapview的apikey
- xml use xslt transform method that is good preformance
- java开发环境搭建---写给java新手
- 超强1000个jquery极品插件!
- VC的Ini文件读写类
- MIME
- CSDN的博客能用多久?
- 大端存储与小端存储
- string.h cstring string的关系
- C#.NET 连接各类数据库 字符串的写法
- PKU_2104_归并树(线段树)
- 取消Visual Studio 2008工具的拼写检查
- [Serializable] 在C#中的作用-NET 中的对象序列化