用WINAPI读写.ini文件
关于
.ini
文件
i
ni
文件是技术人员经常用到的一种系统配置方法,著名的魔兽争霸改键工具魔兽改键精灵就是用
ini
文件存储用户配置的。读写
ini
文件有很多方法,本文主要介绍如何用
Windows API
函数来读写
ini
文件。网上有很多开源爱好者自己编写的用于读写
ini
文件的类,也可以拿来用。
MSDN
里对
ini
文件的简单介绍:
An initialization file. Commonly used in Windows 3.x and earlier, INI files have been used by both the operating system and individual applications to store persistent settings related to an application, driver, or piece of hardware. In later Windows operating systems, INI files are supported for backward compatibility, but the registry is the preferred location for storing such settings.
完全的
ini
文件有
8
个栏目(
column
),为方便起见,只绍较为简单的。
Ini
文件的格式一般如下:
[section1]
key1=value1
key2=value2
......
[section2]
key1=value1
key2=value2 # 注释
......
key1=value1
key2=value2
......
[section2]
key1=value1
key2=value2 # 注释
......
其中
[ ]
里的是块,一个块里有各种不同的键(
key
)和键值(
value
),程序写入和读出的主要就是键值。
例如本例中我自己写的,各个块中的键都相同,比较简单。
[玩家1]
姓名=白色方糖
性别=帅哥
年龄=22
[玩家2]
姓名=雪域剑刃
性别=帅哥
年龄=21
[玩家3]
姓名=守护星
性别=美女
年龄=20
下面是网上找的一个较好的例子:
#ini for path
[path]
dictfile = /home/tmp/dict.dat
inputfile= /home/tmp/input.txt
outputfile= /home/tmp/output.txt
#ini for exe
[exe]
user= winter //user name
passwd= 1234567 #pass word
database= mydatabase
[path]
dictfile = /home/tmp/dict.dat
inputfile= /home/tmp/input.txt
outputfile= /home/tmp/output.txt
#ini for exe
[exe]
user= winter //user name
passwd= 1234567 #pass word
database= mydatabase
其中,#或//表示注释。
ini
文件的读写
下面介绍用于读写
ini
文件的
WINAPI
函数。查阅
MSDN
,可以操作
ini
文件的函数有
:
INI File Functions
MSLU supports Unicode versions of the following INI file functions.
GetPrivateProfileInt()
GetPrivateProfileSection()
GetPrivateProfileSectionNames()
GetPrivateProfileString()
GetPrivateProfileStruct()
GetProfileInt()
GetProfileSection()
GetProfileString()
WritePrivateProfileSection()
WritePrivateProfileString()
WritePrivateProfileStruct()
WriteProfileSection()
WriteProfileString()
GetPrivateProfileSection()
GetPrivateProfileSectionNames()
GetPrivateProfileString()
GetPrivateProfileStruct()
GetProfileInt()
GetProfileSection()
GetProfileString()
WritePrivateProfileSection()
WritePrivateProfileString()
WritePrivateProfileStruct()
WriteProfileSection()
WriteProfileString()
这里我主要说的是
WritePrivateProfileString()
、
GetPrivateProfileString()
和
GetProfileInt()
三个函数,其它函数类似。
写入函数
WritePrivateProfileString
()的原型为
:
BOOL SetPrivateObjectSecurityEx(
SECURITY_INFORMATION SecurityInformation,
PSECURITY_DESCRIPTOR ModificationDescriptor,
PSECURITY_DESCRIPTOR* SecurityDescriptor,
ULONG AutoInheritFlags,
PGENERIC_MAPPING GenericMapping,
HANDLE Token
);
参数的意义:
LPCTSTR lpAppName 是INI文件中的一个字段名.
LPCTSTR lpKeyName 是lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
下面看看这个函数的用法
CString
section,cstrName,cstrTemp,cstrSex;
int
iAge;
section="
玩家1";
cstrName="
白色方糖";
cstrSex="
帅哥";
iAge=22;
WritePrivateProfileString(section,"
姓名", cstrName[i],
".//Playe
rInfo.ini");
WritePrivateProfileString(section,"
性别", cstrSex[i],
".//Playe
rInfo.ini");
cstrTemp.Format("%d",iAge);
WritePrivateProfileString(section,"
年龄",cstrTemp,
".//Playe
rInfo.ini");
其中.//表示相对路径,也可以使用绝对路径。
读取函数
GetPrivateProfileString()
的原型为
DWORD
GetPrivateProfileString(
LPCTSTR
lpAppName
,
LPCTSTR
lpKeyName
,
LPCTSTR
lpDefault
,
LPTSTR
lpReturnedString
,
DWORD
nSize
,
LPCTSTR
lpFileName
);
其中各参数的意义:
前二个参数与 WritePrivateProfileString中的意义一样.
lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.
下面是应用实例,我们接 着读取刚刚存在PlayerInfo.ini里的信息:
CString
strName,strSex;
int
int_age;
GetPrivateProfileString(section,"
姓名","姓名不详", strName.GetBuffer(MAX_PATH),MAX_PATH,".//PlayerInfo.ini");
GetPrivateProfileString(section,"
性别","性别不详", strSex.GetBuffer(MAX_PATH),MAX_PATH,".//PlayerInfo.ini");
int_age=GetPrivateProfileInt(section,"
年龄",20,".//PlayerInfo.ini");
我们注意到当我们要读了取整形数字时,采用了另外一个函数GetPrivateProfileInt(),并且细心的读者会发现,这个函数不再把读取的值存放在参数中,而是作为返回值,使用时要注意这一点。该函数的原型为
UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);
各参数的意义与前面例子类似,不再赘述。
好了到这里我要说的也基本说完了,其它几个函数的用法跟这几个差不多,读者可以查阅MSDN。第一次写教程,手忙脚乱的,水平有限难免有不妥之处,还请读者多多指教;写得不好,欢迎扔臭鸡蛋烂番茄,最好是番茄炒鸡蛋呵呵!