讀取寫入ini檔案

2021-06-08 15:25:31 字數 1823 閱讀 8064

1.所用的winapi函式原型為:

其中各引數的意義:    

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檔案中讀入程式中的變數.

其中各引數的意義:    

前二個引數與 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 的值為:

"張三",若前兩個引數有誤,其值為:"預設姓名".   

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",ncount); ::writeprivateprofilestring("filecount","count",strtemp,"c:\\usefile\\usefile.ini");

file://將檔案總數寫入,以便讀出.

2.讀出: ncount=::getprivateprofileint("filecount","count",0,"c:\\usefile\\usefile.ini");

for(i=0;i   補充四點:    1.ini檔案的路徑必須完整,檔名前面的各級目錄必須存在,否則寫入不成功,該函式返回 false 值.    2.檔名的路徑中必須為 \\ ,因為在vc++中, \\ 才表示乙個 \ .  

3.也可將ini檔案放在程式所在目錄,此時 lpfilename 引數為: ".\\student.ini".

QSetting控制ini檔案寫入和讀取

表示在baseset下的鍵 define vardir language baseset languagetype define vardir rev ip type baseset reviptype define vardir is file segment baseset isfilesegm...

C 建立INI檔案,寫入並可讀取

基於c winform設計。首先建立乙個類,我命名為inifiles。並引入命名空間using system.runtime.interopservices 接著,宣告api函式 dllimport kernel32 private static extern long writeprivatepr...

讀取INI檔案

讀取ini檔案 ini ini new ini using system using system.text using system.runtime.interopservices namespace qf public string path 引用動態連線庫方法 dllimport kernel...