ini檔案三之操作

2021-07-05 13:02:38 字數 4237 閱讀 1945

前些天見有個網友問怎麼用api來實現對ini檔案的讀寫,這個問題我也早就想實現一下,可一直沒有做,現在終於又多了乙個理由來研究它了

用api寫ini檔案的函式有

bool writeprivateprofilestring(

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 lpstring, // 寫入的字串

lpctstr lpfilename  // pointer to string with filename

);用api讀ini檔案的函式有

dword getprivateprofilestring(

lpctstr lpkeyname, // points to key name

lpctstr lpdefault, // 預設字串 ,如果沒有則返回該值

lptstr lpreturnedstring, // 返回的字串

dword nsize, // 返回字串的大小

lpctstr lpfilename  // points to initialization filename

);dword getprivateprofilesection(

lptstr lpreturnedstring, // address of return buffer

dword nsize, // size of return buffer

lpctstr lpfilename  // address of initialization filename  

);uint getprivateprofileint(

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;

varper: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;

varbuffer: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函式,在最下面的那個段**就是

c 之操作ini檔案

public class win32api stringsplitoptions.removeemptyentries 釋放記憶體 marshal.freecotaskmem preturnedstring return sections 獲取ini檔案中指定節點 section 中的所有條目 ke...

彙編學習三 操作INI檔案

filename ini demo.a function demo the operation of ini file author purple endurer log 2005 05 26 created 386 model flat,stdcall option casemap none in...

ini檔案操作

uses inifiles 寫入 varfilename string fileini tinifile begin filename extractfilepath paramstr 0 connect.ini fileini tinifile.create filename fileini.wr...