MFC 讀取配置檔案ini

2021-06-23 05:18:02 字數 2170 閱讀 9913

一.將資訊寫入.ini檔案中.

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

bool writeprivateprofilestring(

lpctstr lpkeyname,

lpctstr lpstring,

lpctstr lpfilename

); 其中引數:

lpctstr lpstring 是鍵值,也就是變數的值,不過必須為lpctstr型或cstring型的.

lpctstr lpfilename 是完整的ini檔名.

2.具體使用方法:設現有一名學生,需把他的姓名和年齡寫入 c:\st \st ent.ini 檔案中.

cstring strname,strtemp;

int nage;

strname="張三";

nage=12;

::writeprivateprofilestring("st entinfo","name",strname,"c:\\st \\st ent.ini");

此時c:\st \st ent.ini檔案中的內容如下:

[st entinfo]

name=張三

3.要將學生的年齡儲存下來,只需將整型的值變為字元型即可:

strtemp.format("%d",nage);

::writeprivateprofilestring("st entinfo","age",strtemp,"c:\\st \\st ent.ini");

二.將資訊從ini檔案中讀入程式中的變數.

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

dword getprivateprofilestring(

lpctstr lpkeyname,

lpctstr lpdefault,

lptstr lpreturnedstring,

dword nsize,

lpctstr lpfilename

); 其中各引數的意義:

前二個引數與 writeprivateprofilestring中的意義一樣.

lpdefault : 如果ini檔案中沒有前兩個引數指定的欄位名或鍵名,則將此值賦給變數.

lpreturnedstring : 接收ini檔案中的值的cstring物件,即目的快取器.

nsize : 目的快取器的大小.

lpfilename : 是完整的ini檔名.

2.具體使用方法:現要將上一步中寫入的學生的資訊讀入程式中.

cstring strst name;

int nst age;

getprivateprofilestring("st entinfo","name","預設姓名",strst name.getb?r(max_path),max_path,"c:\\st \\st ent.ini");

執行後 strst name 的值為:"張三",若前兩個引數有誤,其值為:"預設姓名".

3.讀入整型值要用另乙個winapi函式:

uint getprivateprofileint(

lpctstr lpkeyname,

int ndefault,

lpctstr lpfilename

); 這裡的引數意義與上相同.使用方法如下:

nst age=getprivateprofileint("st entinfo","age",10,"c:\\st \\st ent.ini");

三.迴圈寫入多個值,設現有一程式,要將最近使用的幾個檔名儲存下來,具體程式如下:

1.寫入:

cstring strtemp,strtempa;

int i;

int ncount=6;

檔案://共有6個檔名需要儲存

for(i=0;i

strtemp.format("%d",ncount);

::writeprivateprofilestring("filecount","count",strtemp,"c:\\usefile\\usefile.ini");

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

2.讀出:

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

for(i=0;i

MFC利用系統API讀取INI配置檔案

confing.ini中的內容 賬號 account 123 456 是否修理裝備 repair 1 讀取ini字串 cstring pourin getprivateprofilestring l 賬號 l account null,pourin.getbuffersetlength max pa...

MFC 讀寫配置檔案 ini

1 寫ini檔案 把student.ini 放到c盤根目錄下,路徑也可以在程式裡的兩個函式調整 bool writeprivateprofilestring lpctstr lpkeyname,lpctstr lpstring,lpctstr lpfilename 其中各引數的意義 lpctstr ...

MFC操作ini配置檔案

mfc操作ini配置檔案 vcmfc如何設定對話方塊背景顏色 1.過載onctlcolor cdc pdc,cwnd pwnd,uint nctlcolor 即wm ctlcolor訊息。在cexampledlgdlg的標頭檔案中,新增一cbrush的成員變數 class cexampledlgdl...