.ini文件的介绍及对其进行操作

.ini文件其实跟.txt文件是差不多的,只不过它有自己的一套读取方式,对.ini文件进行操作也有很多方法,而且现在网上还有很多人已经把它写成了一个类,可以方便的对.ini文件进行操作,我这里介绍的是在WIN32控制台项目的平台下进行对非win.ini文件进行操作。

 经典格式:

INI文件的格式很简单,最基本的三个要素是:parameters,sections和comments。

什么是parameters?

INI所包含的最基本的“元素”就是parameter;每一个parameter都有一个name和一个value,name和value是由等号“=”隔开。name在等号的左边。

如:

      name = value

什么是sections ?

所有的parameters都是以sections为单位结合在一起的。所有的section名称都是独占一行,并且sections名字都被方括号包围着([ and ])。在section声明后的所有parameters都是属于该section。对于一个section没有明显的结束标志符,一个section的开始就是上一个section的结束,或者是end of the file。Sections一般情况下不能被nested,当然特殊情况下也可以实现sections的嵌套。

section如下所示:

         [section]

 

什么是comments ?

在INI文件中注释语句是以分号“;”开始的。所有的所有的注释语句不管多长都是独占一行直到结束的。在分号和行结束符之间的所有内容都是被忽略的。

注释实例如下:

;comments text

下面先介绍几个函数,它们的头文件是windows.h

一、对.ini文件写入数据:

函数原型:

BOOL WritePrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpString,
LPCTSTR lpFileName
);

 其中各参数的意义:
LPCTSTR lpAppName INI文件中的一个字段名.
LPCTSTR lpKeyName lpAppName下的一个键名,通俗讲就是变量名.
LPCTSTR lpString 是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.
LPCTSTR lpFileName 是完整的INI文件名.
2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 c:\stud\student.ini 文件中.

CString strName,strTemp;
strName="ZhangSan
";
::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");

此时 c:\stud\student.ini文件中的内容如下:


要将整型的数据保存,将整型的转换为串型的,然后向上面那样操作就行了

.将信息从INI文件中读入程序中的变量.
1.所用的WINAPI函数原型为:

DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);

其中各参数的意义:
前二个参数与 WritePrivateProfileString中的意义一样.
lpDefault : 如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.
lpReturnedString : 接收INI文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI文件名.
2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.

CString strStudName;
int nStudAge;
GetPrivateProfileString("StudentInfo","Name","
默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");

执行后 strStudName 的值为:"张三",若前两个参数有误,其值为:"默认姓名".
3.读入整型值要用另一个WINAPI函数:

UINT GetPrivateProfileInt(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
);

这里的参数意义与上相同.使用方法如下:

nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");


下面是我自己学习的时候用来测试的WS代码,编译环境VS2003,系统XP

代码
 1 #include "stdafx.h"
 2 #include "windows.h"
 3 #include "atlstr.h"
 4 
 5 int _tmain(int argc, _TCHAR* argv[])
 6 {
 7     LPCTSTR strTemp = "ZhangSan";
 8     CString str;
 9     str = "nihao";
10     int nAge = 12;
11     str.Format("%d",nAge);
12     CString strStudName;
13     int nStudAge;
14     ::WritePrivateProfileString("StudentInfo","Name",strTemp,
15         "C:\\Documents and Settings\\Administrator\\桌面\\noname\\student.ini");
16     ::WritePrivateProfileString("StudentInfo","Age",str,
17         "C:\\Documents and Settings\\Administrator\\桌面\\noname\\student.ini");
18     ::GetPrivateProfileString("StudentInfo","Name","noname",strStudName.GetBuffer(MAX_PATH),MAX_PATH,
19         "C:\\Documents and Settings\\Administrator\\桌面\\noname\\student.ini");
20     nStudAge = ::GetPrivateProfileInt("StudentInfo","Age",10,
21         "C:\\Documents and Settings\\Administrator\\桌面\\noname\\student.ini");
22     printf("%s\n",strStudName);
23     printf("%d\n",nStudAge);
24     getchar();
25     return 0;
26 }

 在当我郁闷,如果parameters相同的时候,要怎么样读取数据,我看了一下.ini文件里面的内容,发现在同一个section下还没有出现过有相同parameters的情况出现

最后附上函数总表

6-11 文本操作CRT函数

函数

含义

GetProfileInt

win.ini文件指定Section中读取一个int属性值

GetProfileSection

win.ini文件指定Section中获取所有的属性

GetProfileString

win.ini文件指定Section中读取一个文本属性值

WriteProfileSection

win.ini文件写入一个Section

WriteProfileString

win.ini文件指定Section中写入一个文本属性值

GetPrivateProfileInt

从指定的INI文件的Section中读取一个int属性值

GetPrivateProfileSection

从指定的INI文件中读取指定的Section

GetPrivateProfileSectionNames

从指定的INI文件中获取所有的Section

GetPrivateProfileString

从指定的INI文件的Section中读取一个文本属性值

GetPrivateProfileStruct

从指定的INI文件的Section中读取一个结构属性值

WritePrivateProfileSection

向指定的INI文件写入一个Section

WritePrivateProfileString

向指定的INI文件的Section中写入一个文本属性值

WritePrivateProfileStruct

向指定的INI文件的Section中写入一个结构属性值

原文地址:https://www.cnblogs.com/littlethank/p/1715058.html