写INI文件

前些天见有个网友问怎么用API来实现对INI文件的读写,这个问题我也早就想实现一下,可一直没有做,现在终于又多了一个理由来研究它了
用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);
这句不用多解释吧?
以上就是这些函数的用法。你可能要问“怎么只有写字符串的呀,怎么没有写其它的类型的呢?”,问的好,不过其它类型的用WritePrivateProfileString都能代替,如要写浮点型的就把该类型的放到’’当中,如’12.5’。那位学友又问了,“如果是让用户输入,他们也不知道应该输入什么,怎么能限制他们输入的都是数字”,这方法可太多了,如用控件或检查它们是不是在指定的数字,你可别说不会限制它们是不是数字呀*_^,如果真的不会,你就看它们的ASCII码是不是在48-57之间就行了。“那读出呢?”,Delphi不是有StrToInt,StrToFloat,StrToCurr等这么函数嘛,可以用它们来转换。
我在研究这些函数的同时,发现了一段有趣程序代码,但我不知道它为什么要这样做,有什么好处,大家可以看看它们,不过是用C写的,它们在Delphi SDK或MSDN中查找WritePrivateProfileString函数,在最下面的那个段代码就是

posted @ 2011-11-14 15:56  rookieeeeee  阅读(850)  评论(0编辑  收藏  举报