構造跨平台的Ini檔案類

2021-05-23 05:36:27 字數 3441 閱讀 6357

ini檔案在儲存軟體的配置資訊方面有著良好的作用,為了適應跨平台的功能,我們必須放棄微軟的相關api,自己實現乙個ini的檔案讀寫操作類。 下面我給出該類的具體實現。我的email是

[email protected]

(一) 標頭檔案      h

//ini檔案 以";"表示注釋行

class cinifile ;

(二) 實現檔案  cpp

static const int q_ini_size = 0x4000;

cinifile::cinifile(const char* filename)

}cinifile::~cinifile()

void cinifile::load(const char* filename)

void cinifile::clearcontents()

}bool cinifile::load_ini_buf(char* buf,int *len)

buf[i] = '/0';

*len = i;

ret = true; }

fclose(f); }

return ret; }

bool  cinifile::parse_ini_buf(const char* buf,const char* section,const char* key,

int *s_0,int *s_1,int *k_0,int *k_1,int *v_0,int *v_1)

while( p[i] !=']' && p[i] != '/0');

if (strncmp(p+s_i,section,i-s_i)==0 && strlen(section) ==(i-s_i) )

*s_0 = s_i;

*s_1 = i;

//section symbol end      

while ((p[i] != '/0') &&  !(p[i] =='[' && (p[i-1] == '/r' || p[i-1] == '/n')) )//new line end

m = t;

if (p[m] != ';' || ( (m < i - 1) && p[m] != '/' && p[m+1] !='/' ))//skip comment

} f_not_find:

if (def_value != null)

strncpy(value,def_value,len);

f_exit:

return ret; }

char*  cinifile::readstring(const char* section,const char* key,const char* def_value) ;

readstr(section,key,geo_kd_buf,1024,def_value);

return geo_kd_buf; }

int   cinifile::readint(const char* section,const char* key,const int def_value);

if (!readstr(section,key,v,sizeof(v),null))

return def_value;

else return atoi(v); }

double cinifile::readfloat(const char* section, const char* key,const double def_value);

if (!readstr(section,key,v,sizeof(v),null))

return def_value;

else return atof(v); }

bool cinifile::readbool(const char* section,const char* key,const bool def_value);

if (!readstr(section,key,v,sizeof(v),null))

return def_value;

else ; }

bool  cinifile::writestring(const char* section,const char* key,const char* value);

char m_buf[q_ini_size]=;

int  vlen = strlen(value);

bool ret = false;

if (section!= null && key != null && value !=null)

else

}else if (k_0 == -1) //section exist and key not exist

else

file *f = fopen(m_filename,"w");

if (f != null)

fclose(f); }

ret = true; }

f_exit:

return ret; }

bool  cinifile::writeint(const char* section,const char* key,const int value); 

sprintf(v,"%d",value);

return writestring(section,key,v); }

bool  cinifile::writefloat(const char* section,const char* key,const double value);

sprintf(v,"%f",value);

return writestring(section,key,v); }

bool  cinifile::writebool(const char* section,const char* key,const bool value);

if (value==true)

else memcpy(v,"false",5);

return writestring(section,key,v); }

void  cinifile::erasesection(const char* section);

char m_buf[q_ini_size]=;

if (section!= null)

fclose(f); }

} }f_exit:

return; }

使用方法:

cinifile* ini = new cinifile("d://wxy3064.ini");

或者 cinifile* ini = new cinifile();

ini ->load("d://wxy3064.ini");

ini->writestring("coltheme","type","gsm");

ini->writeint("coltheme","value",3);

delete ini;

C C 跨平台INI檔案讀寫API

已測試通過的開發環境 winxp vista vc6.0 vs2003 vs2005 vs2008 fc6.0 fc7.0 ubuntu7.10 gcc4.1 arm linux arm linux gcc3.3.2 專案特點 1.使用標準c庫函式,支援windows linux unix等多平台。...

ini檔案操作類

using system using system.io using system.runtime.interopservices using system.text using system.collections using system.collections.specialized name...

InI檔案工具類

ini檔案格式 這裡以分號開頭的都是注釋 section value using system using system.collections.generic using system.linq using system.text using system.web using system.run...