c ini 檔案的讀寫及相關函式

2021-09-28 10:44:20 字數 4276 閱讀 9157

bool readecryflag(cstring filepath, int &rflag)

; int flag = -1;

getprivateprofilestring(_t("encryflag"),_t("isencry"),null,(lpwstr)szflag,256, filepath);

flag = atoi(szflag);

rflag = flag;

return true;

}bool writeecryflag(cstring filepath,const char* wflag)

bool writeecryinfo(cstring filepath, wchar *flag,wchar * wflag)

ini檔案格式

;[encryflag]

isencry=1

;登入資訊

[loginmessage]

user=

password=

在unicode環境下,需要使用對應的資料型別(wchar),不然會找不到標籤,和亂碼

我們可能經常用到配置檔案ini檔案來獲取或者儲存引數資訊,在vc中其函式中主要用到的有:

讀取讀取字元

dword getprivateprofilestring(

lpctstr lpdefault, // 如果lpreturnedstring為空,則把個變數賦給lpreturnedstring

lptstr lpreturnedstring, // 存放鍵值的指標變數,用於接收ini檔案中鍵值(資料)的接收緩衝區

dword nsize, // lpreturnedstring的緩衝區大小

lpctstr lpfilename // ini檔案的路徑

);讀取整型值 (返回值為讀到的整)

uint getprivateprofileint(

int ndefault, // 如果沒有找到指定的資料返回,則把個變數值賦給返回值

lpctstr lpfilename // ini檔案的路徑

);寫入

寫入字元

bool writeprivateprofilestring(

lpctstr lpstring, // 鍵值,也就是資料

lpctstr lpfilename // ini檔案的路徑

);寫入整型

讀寫ini檔案時相對路徑和絕對路徑都可以,根據實際情況選擇

"..\\inifilename.ini" // 這樣的為相對路徑

"d:\\inifilename.ini" // 這樣的為絕對路徑

max_path:是微軟最大路徑佔的位元組所設的巨集

寫入的ini檔案 分為:節、鍵名、鍵值

舉個栗子:

// xx.ini

[串列埠配置]

波特率=19200

有了理論,再看看實踐的demo:

寫ini檔案:

lptstr lppath = new char[max_path];

strcpy(lppath, "d:\\inifilename.ini");

writeprivateprofilestring("liming", "***", "man", lppath);

writeprivateprofilestring("liming", "age", "20", lppath);

writeprivateprofilestring("fangfang", "***", "woman", lppath);

writeprivateprofilestring("fangfang", "age", "21", lppath);

delete lppath;

ini檔案如下:

[liming]

***=man

age=20

[fangfang]

***=woman

age=21

讀ini檔案:

tchar fpath[128];

::getcurrentdirectory(max_path, fpath);

strcat(fpath, ".\\config.ini");

cfile configfile;

if(!configfile.open(fpath, cfile::modecreate||cfile::moderead, null))

configfile.close();

lptstr lppath = new char[max_path];

lptstr liming*** = new char[6];

int limingage;

lptstr fangfang*** = new char[6];

int fangfangage;

strcpy(lppath, "..\\inifilename.ini");

getprivateprofilestring("liming", "***", "", liming***, 6, lppath);

limingage = getprivateprofileint("liming", "age", 0, lppath);

getprivateprofilestring("fangfang", "***", "", fangfang***, 6, lppath);

fangfangage = getprivateprofileint("fangfang", "age", 0, lppath);

delete lppath;

配置檔案中經常用到ini檔案,在vc中其函式分別為:

其中個引數的意思:

lpctstr lpstring ---------是鍵值,也就是變數的值, 必須為lpctstr或cstring型別

lpctstr lpfilename --------完整的ini檔案路徑名

lpctstr lpdefaut ----------如果沒有其前兩個引數值,則將此值賦給變數

lpstr lpreturnedstring --------接收ini檔案中的值的cstring物件,即接收緩衝區

dword nsize ------接收緩衝區的大小

例子:cstring strname,strtemp;

int nage;

strname = "jacky";

nage = 13;

writeprivateprofilestring("student","name",strname,"c:\\setting.ini");

結果:(ini檔案中顯示如下:)

[student]

name=jacky

讀取:cstring sname;

getprivateprofilestring("student","name","defaultname",sname.getbuffer(max_length),max_length,"c:\\setting.ini");

結果:sname = "jacky";這裡需要注意點就是用完getbuffer函式後一定要釋放(用sname.releasebuffer()函式),不然後面再用到sname的其他子函式就會失靈。

讀整數比較簡單,如下

int result = getprivateprofileint("student","nage",0,"c:\\setting.ini")返回值即為所讀取的結果!

在getprivateprofilestring最後乙個引數是配置檔案路徑的引數,此路徑只能是絕對路徑,不能是相對路徑,但現在我需要是我的exe檔案能和我的配置檔案在一起。因此我使用了getcurrentdirectory函式。

原**如下:

cstring server_ip;

cstring des="";

::getcurrentdirectory(max_pathlength,des.getbuffer(max_pathlength));

des.releasebuffer();

des+="\\config.ini";

getprivateprofilestring("phonedemo","server_ip","",server_ip.getbuffersetlength(15),15,des);

server_ip.releasebuffer();

注意:在這裡使用cstring變數時,在使用完getbuffer後,緊接著一定要使用releasebuffer()函式,才可以進行其他的諸如字串+操作;

C ini 檔案的讀寫

先 參考 之前的乙個博文 配置檔案中經常用到ini檔案,在vc中其函式分別為 寫入.ini檔案 bool writeprivateprofilestring lpctstr lpstring,鍵值,也就是資料 lpctstr lpfilename ini檔案的路徑 讀取.ini檔案 dword ge...

C ini檔案讀寫類

vc中提供了api函式進行ini檔案的讀寫操作,但是微軟推出的c 程式語言中卻沒有相應的方法,下面是乙個c ini檔案讀寫類,從網上收集的,很全,就是沒有對section的改名功能,高手可以增加乙個。using system using system.io using system.runtime....

C ini檔案讀寫 例項

ini檔案一般用於儲存當前執行的程式或者一些臨時的配置屬性的檔案。也有時用於儲存一定的資料以便於臨時或者配置上的需要。文字格式如下 section1 name 用 括起來,其包含多個key keyname1 value1 格式是 key value。keyname2 value2 section2 ...