您的位置:首页 > 其它

操作INI文件的函数(转载)晚上修改下

2008-10-09 11:19 211 查看
用API写INI文件的函数有

BOOL WritePrivateProfileString(

LPCTSTR lpAppName, // 节名

LPCTSTR lpKeyName, // 键名

LPCTSTR lpString, // 添加的字符串

LPCTSTR lpFileName // Ini文件名

);

BOOL WritePrivateProfileStruct(

LPCTSTR lpszSection, // pointer to section name

LPCTSTR lpszKey, // pointer to key name

LPVOID lpStruct, // 要写入的数据缓冲区

UINT uSizeStruct, // 缓冲区的大小

LPCTSTR szFile // pointer to initialization filename

);

BOOL WritePrivateProfileSection(

LPCTSTR lpAppName, // pointer to string with section name

LPCTSTR lpString, // 写入的字符串

LPCTSTR lpFileName // pointer to string with filename

);

用API读INI文件的函数有

DWORD GetPrivateProfileString(

LPCTSTR lpAppName, // points to section name

LPCTSTR lpKeyName, // points to key name

LPCTSTR lpDefault, // 默认字符串 ,如果没有则返回该值

LPTSTR lpReturnedString, // 返回的字符串

DWORD nSize, // 返回字符串的大小

LPCTSTR lpFileName // points to initialization filename

);

DWORD GetPrivateProfileSection(

LPCTSTR lpAppName, // address of section name

LPTSTR lpReturnedString, // address of return buffer

DWORD nSize, // size of return buffer

LPCTSTR lpFileName // address of initialization filename

);

UINT GetPrivateProfileInt(

LPCTSTR lpAppName, // address of section name

LPCTSTR lpKeyName, // address of key name

INT nDefault, // return value if key name is not found

LPCTSTR lpFileName // address of initialization filename

);

BOOL GetPrivateProfileStruct(

LPCTSTR lpszSection, // address of section name

LPCTSTR lpszKey, // address of key name

LPVOID lpStruct, // address of return buffer

UINT uSizeStruct, // size of return buffer

LPCTSTR szFile // address of initialization filename

);

DWORD GetPrivateProfileSectionNames(

LPTSTR lpszReturnBuffer, // address of return buffer

DWORD nSize, // size of return buffer

LPCTSTR lpFileName // address of initialization filename

);

当然还有如WriteProfileString,WriteProfileSection,WriteProfileSturct, GetProfileString,GetProfileStruct,GetProfileSectionNames,GetProfileInt,GetProfileSection但这些只对Win.ini有效

下面我们来学习它们的用法

WritePrivateProfileString函数是向ini文件中写入字符串,如

WritePrivateProfileString(Pchar('类型'),Pchar('API'),Pchar('API 真好!'),Pchar('c:\example.ini'));

如果第二个参数是nil,那么该操作将删除该节

如果第三个参数为nil,那么该操作将删除该节中的所有键

如果在指定的文件中没有路径,那么它将在系统的目录寻找文件,如果不存在则建立

WritePrivateProfileSection是向文件中写入一整个键,其它键的格式为key = value,如

WritePrivateProfileSection(Pchar('类型'),Pchar('其它=123'),Pchar('c:\example.ini'));

注意,该操作将删除该节中的所有键后在进行本次的写入

WritePrivateProfileStruct是向文件中写入一个结构,如

type

TPerson = record

Name:string;

Age:integer;

end;

var

Per:TPerson;

WritePrivateProfileStruct(Pchar('类型'),Pchar('结构'),@Per,Sizeof(Per),Pchar('C:\example.ini'));

GetPrivateProfileString是从文件中读出一个指定键的字符串,如果没有找到,则返回默认值

GetPrivateProfileString(Pchar(‘类型'),Pchar('API'),Pchar('no value'),Str1,21,Pchar('c:\example.ini'));

GetPrivateProfileSection是从文件中读出一整个键

GetprivateProfileSection('类型',str1,200,'c:\example.ini');

GetPrivateProfileInt是从文件中读出指定键的整型值,如果没找到或不是整型的值,则返回默认值,如果返回值小于0,则返回0,如

i:=GetPrivateProfileInt(Pchar('类型'),Pchar('整型'),i,Pchar('C:\example.ini'));

showMessage(inttostr(i));

GetPrivateProfileStruct从文件中读出指定键的结构值,如

type

TPerson = record

Name:string;

Age:integer;

end;

var

Buffer:TPerson;

GetPrivateProfileStruct(Pchar('类型'),Pchar('结构'),@Buffer,Sizeof(Per),Pchar('C:\example.ini'));

GetPrivateProfileSectionNames是从文件中读出所有节的名称,该函数返回读入缓冲区的字符数,如

count:=GetPrivateProfileSectionNames(Str3,200,Pchar('c:\example.ini'));

ShowMessage(Str3);

此处显示的只是第一个节的名称,如果要显示第二个字符的名称则要用到下面这句

showmessage(str3+5);

这句不用多解释吧?

VC中操作INI文件的函数主要有:

函数名功能备注
GetPrivateProfileInt 读取INI文件指定块中的键名对应的整数值。
GetPrivateProfileSection记取INI文件指定块中的所有键名及其对应值。
GetPrivateProfileSectionNames读取一INI文件中所有的块名。
GetPrivateProfileString读取INI文件指定块中的键名对应的字符串。
GetPrivateProfileStruct读取INI文件指定块中的键名对应的数据
GetProfileInt读取win.ini中指定块中的键名对应的整数值。
GetProfileSection读取win.ini中指定块中所有的键名及其值。
GetProfileString读取win.ini中指定块中的键名的对应值。

WritePrivateProfileSection替换INI文件中指定块中所有键名对应的值。
WritePrivateProfileString把给定的键名及其值写入到指定INI文件的相应块中。
WritePrivateProfileStruct把指定的键名及其数据写入到指定INI文件的块中。
WriteProfileSection替换win.ini中指定块的所有键名对应的值。
WriteProfileString将给定的键名及值写入win.ini中对应的块中。
INI文件可用来保存,共享应用程序中少量的数据,其格式一般为:

[块名1]

键名=值

...

[块名n]

键名=值

一般一个INI文件可有N个块,第块可有n个键名及值对应。每个键名及其值占一行。

一般键的名称可任取,不过建议用有意义的字符及词构成。值一般可为整数和字符串,其它类型要进行转换。

GetProfileInt - 从 Win.ini 文件的某个 Section 取得一个 key 的整数值,它的原形是:

GetProfileInt(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

INT nDefault // 如果 Key 值没有找到,则返回缺省的值是多少

);

如果 Key 值没有找到的话,返回值是 nDefault 指定的缺省值,如果 Key 中的值是负数,则返回 0,如果 Key 指定的是数字和字符串的混合,则返回数字部分的值,比如说 x=1234abcd,则返回 1234

GetProfileString - 从 Win.ini 文件的某个 Section 取得一个 key 的字符串,它的原形是:

GetProfileString(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

LPCTSTR lpDefault, // 如果 Key 值没有找到,则返回缺省的字符串的地址

LPTSTR lpReturnedString, // 返回字符串的缓冲区地址

DWORD nSize // 缓冲区的长度

);

返回的字符串在缓冲区内,返回的 eax 值是返回的字符串的长度(不包括尾部的0)

GetProfileSection - 从 Win.ini 文件中读出整个 Section 的内容,它的原形是:

GetProfileSection(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPTSTR lpReturnedString, // 返回数据的缓冲区地址

DWORD nSize // 返回数据的缓冲区长度

);

WriteProfileSection - 将一个整个 Section 的值 写入 Win.ini 文件的指定 Section 中,它的原形是:

WriteProfileSection(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpString // 要写入的数据的地址

);

如果 Win.ini 没有指定的 Section,API 会新建立一个并写入数据,如果已经存在,则先删除原来 Seciton 中所有的 Key 值然后写入新的。

WriteProfileString - 将一个 Key 值写入 Win.ini 文件的指定 Section 中,它的原形是:

WriteProfileString(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

LPCTSTR lpString // 要写的字符串地址

);

如果 Win.ini 没有指定的 Section,API 会新建 Section,如果没有指定的 Key 则新建一个 Key 并写入数据,如果已经存在,则用字符串代替原来的值。

以上的 Api 是对 Win.ini 操作的,当然对于我们来说,用的更多的是在程序运行的目录中建立自己的 ini 文件,如果需要对自己的 ini 文件操作,就要用到另一组 Api,这一组 api 和上面的很象,只要把上面一组的 Profile 换成 PrivateProfile(私有的)就可以了,参数中也相应的多了一个 ini 文件名的参数。例如 GetPrivateProfileInt、GetPrivateProfileSection、WritePrivateProfileString 等等, 下面分别介绍:

GetPrivateProfileInt - 从 ini 文件的某个 Section 取得一个 key 的整数值,它的原形是:

GetPrivateProfileInt(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

INT nDefault // 如果 Key 值没有找到,则返回缺省的值是多少

LPCTSTR lpFileName // ini 文件的文件名

);

中间参数和返回值的定义和 GetProfileInt 是一样的。

GetPrivateProfileString - 从 ini 文件的某个 Section 取得一个 key 的字符串,它的原形是:

GetPrivateProfileString(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

LPCTSTR lpDefault, // 如果 Key 值没有找到,则返回缺省的字符串的地址

LPTSTR lpReturnedString, // 返回字符串的缓冲区地址

DWORD nSize // 缓冲区的长度

LPCTSTR lpFileName // ini 文件的文件名

);

GetPrivateProfileSection - 从 ini 文件中读出整个 Section 的内容,它的原形是:

GetPrivateProfileSection(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPTSTR lpReturnedString, // 返回数据的缓冲区地址

DWORD nSize // 返回数据的缓冲区长度

LPCTSTR lpFileName // ini 文件的文件名

);

这个 api 可以读出整个 section 的内容,当你不知道 section 中有哪些 key 的时候,可以使用这个 api 将整个 section 读出后再处理。

说明:

例ini文件为:

[student]

Name=tian

Age=20

Sex=man

由GetPrivateProfileSection()函数第二个参数lpReturnedString 返回的字符串如下:

Name=tian"0Age=20"0Sex=man"0"0(每条以"0分割,最后以两个"0结束)

而我们是想等到每个key及其对应的值,那怎么分割上面那个字符串呢?在VB有中个叫Split()函数,用起来很方便,但是vc里面没有。

当然,你也可以在VC中去实现一个Split()函数,不过我在这里提供一个简单的方法:

CString strKey;

while(*str!='"0') //str为lpReturnedString返回的字符串

{

strKey = str;

MessageBox(strKey); //strKey为每个key及其对应的值

str += strKey.GetLength()+1;

}

GetPrivateProfileSectionNames - 从 ini 文件中获得 Section 的名称,它的原形是:

GetPrivateProfileSectionNames(

LPTSTR lpszReturnBuffer, // 返回数据的缓冲区地址

DWORD nSize // 返回数据的缓冲区长度

LPCTSTR lpFileName // ini 文件的文件名

);

如果 ini 中有两个 Section: [sec1] 和 [sec2],则返回的是 'sec1',0,'sec2',0,0 ,当你不知道 ini 中有哪些 section 的时候可以用这个 api 来获取名称

WritePrivateProfileSection - 将一个整个 Section 的内容入 ini 文件的指定 Section 中,它的原形是:

WritePrivateProfileSection(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpString // 要写入的数据的地址

LPCTSTR lpFileName // ini 文件的文件名

);

WritePrivateProfileString - 将一个 Key 值写入 ini 文件的指定 Section 中,它的原形是:

WritePrivateProfileString(

LPCTSTR lpAppName, // 指向包含 Section 名称的字符串地址

LPCTSTR lpKeyName, // 指向包含 Key 名称的字符串地址

LPCTSTR lpString // 要写的字符串地址

LPCTSTR lpFileName // ini 文件的文件名

);

如果 ini 中没有指定的 Section,API 会新建 Section,如果没有指定的 Key 则新建一个 Key 并写入数据,如果已经存在,则用字符串代替原来的值。当指定的 ini 也不存在的时候,API 会自动建立一个新的文件,所以使用 ini 的好处是我们不必为了保存少量的数据涉及到文件操作,就连查找文件是否存在的操作都不必要。

使用要点:

在我们实际使用的时候,用的最多的是 GetPrivateProfileString 和 WritePrivateProfileString,但在对自定义 ini 文件操作的时候要注意的是,如果 lpFileName 指定的文件没有路径的话,Api 会去 Windows 的安装目录去找而不会在当前目录找,但是每次用到 ini 函数要获取当前路径显然太麻烦了,这里有一个变通的办法,你只要在 ini 文件名前面加上 ." 就可以了,比如说要对本目录下的 user.ini 操作,那么文件名就是 '."user.ini' 这样显然比较方便。另外,当你要把一个 Key 清除的时候,可以使用把 lpString 指向一个空的字符串然后使用 WritePrivateProfileString。当你要把一个 section 的全部内容清空的时候,也不必把 key 一个个的清除,可以使用把 lpString 指向一个空的字符串然后使用 WritePrivateProfileSection。

--------------------------------

在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下:

  一.将信息写入.INI文件中.

  1.所用的WINAPI函数原型为:

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;

int nAge;

strName="张三";

nAge=12;

::WritePrivateProfileString("StudentInfo","Name",strName,

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

   [StudentInfo]

   

  3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:

strTemp.Format("%d",nAge);

::WritePrivateProfileString("StudentInfo","Age",strTemp,

"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");三.循环写入多个值,设现有一程序,要将最近使用的几个文件名保存下来,具体程序如下:

  1.写入:

CString strTemp,strTempA;

int i;

int nCount=6;

file://共有6个文件名需要保存

for(i=0;i {strTemp.Format("%d",i);

strTempA=文件名;

file://文件名可以从数组,列表框等处取得.

::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,

"c:""usefile""usefile.ini");

}

strTemp.Format("%d",nCount);

::WritePrivateProfileString("FileCount","Count",strTemp,

"c:""usefile""usefile.ini");

file://将文件总数写入,以便读出.  2.读出:

nCount=::GetPrivateProfileInt("FileCount","Count",0,

"c:""usefile""usefile.ini");

for(i=0;i {strTemp.Format("%d",i);

strTemp="FileName"+strTemp;

::GetPrivateProfileString("CurrentIni",strTemp,

"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,

"c:""usefile""usefile.ini");

file://使用strTempA中的内容.

}  

补充四点:

   1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 FALSE 值.

   2.文件名的路径中必须为 "" ,因为在VC++中, "" 才表示一个 " .

   3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: “.""student.ini”.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: