MFC操作ini檔案方法

2021-07-23 13:19:13 字數 1890 閱讀 8687

在我們的程式設計中經常需要對一些引數進行配置,配置好後還要在下一次啟動仍然有效,那麼乙個有效的可行辦法就是使用ini檔案,也就是windows初始化檔案來儲存一些我們的設定,然後讓程式啟動的時候從這個ini檔案中讀取相關配置。我們需要做以下的工作。

1.建立此ini檔案,windows對ini檔案的操作有專門的函式,我們經常用的就是writeprivateprofilestring()和getprivateprofilestring()了。那麼我們在程式的初始化時首先檢查是否存在ini檔案,如果不存在則建立乙個預設的ini檔案。

我們使用cfilefind類查詢我們需要的ini檔案是否存在,若不存在則建立乙個。writeprivateprofilestring()當ini不存在時會自動建立乙個ini檔案在指定路徑。

判斷檔案是否存在,如果不存在建立檔案並寫入相關資料;

[cpp]view plain

copy

print?

cfilefind finder;   //查詢是否存在ini檔案,若不存在,則生成乙個新的預設設定的ini檔案,這樣就保證了我們更改後的設定每次都可用  

bool iffind = finder.findfile(_t("d:\\qzze.ini"));  

if (!iffind)    

cfilefind finder;   //查詢是否存在ini檔案,若不存在,則生成乙個新的預設設定的ini檔案,這樣就保證了我們更改後的設定每次都可用  

bool iffind = finder.findfile(_t("d:\\qzze.ini"));

if (!iffind)

ini檔案資料:

[plain]view plain

copy

print?

[xwreg]  

ip=10.210.0.9  

database=roadnetdata  

uid=sa  

pwd=4814278  

id=001  

[xwreg]

ip=10.210.0.9

database=roadnetdata

uid=sa

pwd=4814278

id=001

寫入修改資料:

[cpp]view plain

copy

print?

cstring strobject;  

getdlgitemtext(idc_edit1, strobject);  

writeprivateprofilestringw(_t("xwreg"), _t("ip"), strobject, _t("d:\\qzze.ini"));  

cstring strobject;

getdlgitemtext(idc_edit1, strobject);

writeprivateprofilestringw(_t("xwreg"), _t("ip"), strobject, _t("d:\\qzze.ini"));

讀取資料:

[cpp]view plain

copy

print?

cstring str;  

getprivateprofilestring(_t("xwreg"), _t("ip"), cstring("null"), str.getbuffer(max_path), max_path,_t("d:\\qzze.ini"));  

setdlgitemtext(idc_edit2, str);  

MFC操作ini配置檔案

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

MFC操作ini配置檔案

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

MFC建立及讀寫ini檔案操作

判斷檔案是否存在,如果不存在建立檔案並寫入相關資料 cfilefind finder 查詢是否存在ini檔案,若不存在,則生成乙個新的預設設定的ini檔案,這樣就保證了我們更改後的設定每次都可用 bool iffind finder.findfile t d qzze.ini if iffind i...