您的位置:首页 > 其它

VC操作INI文件

2015-06-12 16:06 225 查看
/article/2321260.html

INI文件简介

在我们写程序时,总有一些配置信息需要保存下来,以便在下一次启动程序完成初始化,这实际上是一种类持久化。将一些信息写入INI文件(initialization file)中,可完成简单的持久化支持。
Windows提供了API接口用于操作INI文件,其支持的INI文件格式一般如下:
===============================
[Section1]
Key11=value11
Key12=value12
[Section2]
Key21=value21
Key22=value22
...
[SectionN]
KeyN1=valueN1
KeyN2=valueN2
===============================
一般一个INI文件可有N个节,每节可有n个键名及值对应,每个键名及其值以等式形式占一行。
一般键的名称可任取,不过建议用有意义的字符及词构成。值一般可为整数和字符串,其它类型要进行转换。
常见的系统配置文件:
C:/boot.ini
C:/WINDOWS/win.ini
C:/WINDOWS/system.ini
C:/WINDOWS/desktop.ini
C:/WINDOWS/Resources/Themes/Windows Classic.theme
注意,字符串存贮在INI文件中时没有引号;key和value之间的等号前后不容空格;注释以分号“;”开头。

VC中操作INI文件的API

(1)操作系统配置文件Win.ini的函数:

函数名
功能
GetProfileSection

读取win.ini中指定节lpAppName中所有键名及其值。lpReturnedString字符串形式如下:

Key1=Value1/0Key2=Value2/0…KeyN=ValueN/0/0

GetProfileString

读取win.ini中指定节lpAppName中键名为lpKeyName对应变量的字符串值。

GetProfileInt

读取win.ini中指定节lpAppName中键名为lpKeyName对应变量的整数值。

WriteProfileSection

写(替换)win.ini中指定节lpAppName中的键值。

lpString字符串形式同GetProfileSection中的lpReturnedString。

WriteProfileString

写(替换)win.ini中指定节lpAppName中键名为lpKeyName对应变量的字符串值。

(2)操作用户自定义配置文件(PrivateProfile.ini)的函数:

函数名
功能
GetPrivateProfileSectionNames

读取lpFileName指定的配置文件中所有的节名。lpszReturnBuffer字符串形式如下:

Section1/0Section2/0…SectionN/0/0

GetPrivateProfileSection

同GetProfileSection。

GetPrivateProfileString

同GetProfileString。

GetPrivateProfileInt

同GetProfileInt

GetPrivateProfileStruct

须同WritePrivateProfileStruct配套使用。

WritePrivateProfileSection

同WriteProfileSection

WritePrivateProfileString

同WriteProfileString

WritePrivateProfileStruct

不常用。

注意:

(1)使用得最频繁的是 GetPrivateProfileString 和 WritePrivateProfileString,没有WriteProfileInt/WritePrivateProfileInt函数。
(2)Get系列读取节键值,如果文件路径有误或节键名不对则返回设定的默认值。

(3)访存自定义配置文件时,文件路径lpFileName必须完整,文件名前面的各级目录必须存在。如果lpFileName文件路径不存在,则函数返回FALSE,GetLastError() = ERROR_PATH_NOT_FOUND。如果路径正确,但是文件不存在,则该函数将先创建该文件。如果路径及文件存在,则在现有ini文件基础上进行读写。

如果 lpFileName 只指定文件名而没有路径的话,调用API将会去 Windows 的安装目录去查找而不会在当前目录查找。

(4)要对调用API的模块(exe)所在目录下进行配置文件操作,可使用形如“.//config.ini”的相对路径,注意转义符。
(5)调用WritePrivateProfileSection,若参数三 lpString为NULL,则可将对应section的全部内容清空;调用WritePrivateProfileString,若参数三 lpString为NULL,则可将对应key删除。

跨平台配置文件
INI文件本质是对文件和字符串的处理,因此在跨平台项目中的配置文件可以基于<stdio.h>中的标C文件FILE,然后实现像类似以上对节([Section])、键(Key)和值(Value)的字符串读写功能。
鉴于XML的树形描述层次结构性清晰,现在很多软件都大面积使用XML文件进行配置,如QQ的全局配置文件C:/Program Files/Tencent/QQ/gf-config.xml。java程序的配置文件基本都使用XML格式,C++中并没有操作XML文件的标准库。
在C/C++程序中要使用XML做为配置文件,涉及到XML的解析。Windows平台可使用MsXml对XML进行解析,参考《MsXml创建和解析XML示例》,跨平台可以考虑自己实现,或使用C++ BOOST正则表达式,或选择Free
C or C++ XML Parser Libraries,如XmlParserTinyXMLCMarkuplibxml等。

CIniFile类
以下提供对Windows操作INI文件的API的简单封装类CIniFile。

[cpp] view plaincopyprint?

// IniFile.h
#ifndef __INIFILE_H__
#define __INIFILE_H__

class CIniFile
{
public:
CIniFile();
CIniFile(LPCTSTR szFileName);
virtual ~CIniFile();

public:
// Attributes
void SetFileName(LPCTSTR szFileName);

public:
// Operations
BOOL SetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, int nKeyValue);
BOOL SetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, LPCTSTR lpszKeyValue);

DWORD GetProfileSectionNames(CStringArray& strArray); // 返回section数量

int GetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName);
DWORD GetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, CString& szKeyValue);

BOOL DeleteSection(LPCTSTR lpszSectionName);
BOOL DeleteKey(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName);

private:
CString m_szFileName; // .//Config.ini, 如果该文件不存在,则exe第一次试图Write时将创建该文件

UINT m_unMaxSection; // 最多支持的section数(256)
UINT m_unSectionNameMaxSize; // section名称长度,这里设为32(Null-terminated)

void Init();
};

#endif

// IniFile.cpp
#include "IniFile.h"

void CIniFile::Init()
{
m_unMaxSection = 512;
m_unSectionNameMaxSize = 33; // 32位UID串
}

CIniFile::CIniFile()
{
Init();
}

CIniFile::CIniFile(LPCTSTR szFileName)
{
// (1) 绝对路径,需检验路径是否存在
// (2) 以"./"开头,则需检验后续路径是否存在
// (3) 以"../"开头,则涉及相对路径的解析

Init();

// 相对路径
m_szFileName.Format(".//%s", szFileName);
}

CIniFile::~CIniFile()
{

}

void CIniFile::SetFileName(LPCTSTR szFileName)
{
m_szFileName.Format(".//%s", szFileName);
}

DWORD CIniFile::GetProfileSectionNames(CStringArray &strArray)
{
int nAllSectionNamesMaxSize = m_unMaxSection*m_unSectionNameMaxSize+1;
char *pszSectionNames = new char[nAllSectionNamesMaxSize];
DWORD dwCopied = 0;
dwCopied = ::GetPrivateProfileSectionNames(pszSectionNames, nAllSectionNamesMaxSize, m_szFileName);

strArray.RemoveAll();

char *pSection = pszSectionNames;
do
{
CString szSection(pSection);
if (szSection.GetLength() < 1)
{
delete[] pszSectionNames;
return dwCopied;
}
strArray.Add(szSection);

pSection = pSection + szSection.GetLength() + 1; // next section name
} while (pSection && pSection<pszSectionNames+nAllSectionNamesMaxSize);

delete[] pszSectionNames;
return dwCopied;
}

DWORD CIniFile::GetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, CString& szKeyValue)
{
DWORD dwCopied = 0;
dwCopied = ::GetPrivateProfileString(lpszSectionName, lpszKeyName, "",
szKeyValue.GetBuffer(MAX_PATH), MAX_PATH, m_szFileName);
szKeyValue.ReleaseBuffer();

return dwCopied;
}

int CIniFile::GetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName)
{
int nKeyValue = ::GetPrivateProfileInt(lpszSectionName, lpszKeyName, 0, m_szFileName);

return nKeyValue;
}

BOOL CIniFile::SetProfileString(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, LPCTSTR lpszKeyValue)
{
return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, lpszKeyValue, m_szFileName);
}

BOOL CIniFile::SetProfileInt(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName, int nKeyValue)
{
CString szKeyValue;
szKeyValue.Format("%d", nKeyValue);

return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, szKeyValue, m_szFileName);
}

BOOL CIniFile::DeleteSection(LPCTSTR lpszSectionName)
{
return ::WritePrivateProfileSection(lpszSectionName, NULL, m_szFileName);
}

BOOL CIniFile::DeleteKey(LPCTSTR lpszSectionName, LPCTSTR lpszKeyName)
{
return ::WritePrivateProfileString(lpszSectionName, lpszKeyName, NULL, m_szFileName);
}

参考:
在VC++中读写INI文件
VC INI文件读写
用VC++操作INI文件
INI操作类
INI文件查看器
C++中的XML配置文件编程经验
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: