C++读写ini配置文件实现过程详解
在Windows的VC下
读ini文件
例如:在D:\test.ini文件中
[Font]
name=宋体
size=12pt
color=RGB(255,0,0)
上面的=号两边可以加空格,也可以不加
用GetPrivateProfileInt()和GetPrivateProfileString()
[section] key=string . . 获取integer UINTGetPrivateProfileInt( LPCTSTRlpAppName,//sectionname LPCTSTRlpKeyName,//keyname INTnDefault,//returnvalueifkeynamenotfound LPCTSTRlpFileName//initializationfilename ); 注意:lpAppName和lpKeyName不区分大小写,当获得integer<0,那么返回0。lpFileName必须是绝对路径,因相对路径是以C:\windows\ DWORDGetPrivateProfileString( LPCTSTRlpAppName,//sectionname LPCTSTRlpKeyName,//keyname LPCTSTRlpDefault,//defaultstring LPTSTRlpReturnedString,//destinationbuffer DWORDnSize,//sizeofdestinationbuffer LPCTSTRlpFileName//initializationfilename ); 注意:lpAppName和lpKeyName不区分大小写,若lpAppName为NULL,lpReturnedString缓冲区内装载这个ini文件所有小节的列表,若为lpKeyName=NULL,就在lpReturnedString缓冲区内装载指定小节所有项的列表。lpFileName必须是绝对路径,因相对路径是以C:\windows\, 返回值:复制到lpReturnedString缓冲区的字符数量,其中不包括那些NULL中止字符。如lpReturnedString缓冲区不够大,不能容下全部信息,就返回nSize-1(若lpAppName或lpKeyName为NULL,则返回nSize-2) 获取某一字段的所有keys和values DWORDGetPrivateProfileSection( LPCTSTRlpAppName,//sectionname LPTSTRlpReturnedString,//returnbuffer DWORDnSize,//sizeofreturnbuffer LPCTSTRlpFileName//initializationfilename ); retrievesthenamesofallsectionsinaninitializationfile. DWORDGetPrivateProfileSectionNames( LPTSTRlpszReturnBuffer,//returnbuffer DWORDnSize,//sizeofreturnbuffer LPCTSTRlpFileName//initializationfilename ); 其实就等于,GetPrivateProfileString(NULL,NULL,lpszReturnedBuffer,nSize,lpFileName)
例子:
/*test.ini"="号两边可以加空格,也可以不加 [Font] name=宋体 size=12pt color=RGB(255,0,0) [Layout] [Body] */ CStringstrCfgPath=_T("D:\\test.ini");//注意:'\\' LPCTSTRlpszSection=_T("Font"); intn=GetPrivateProfileInt(_T("FONT"),_T("size"),9,strCfgPath);//n=12 CStringstr; GetPrivateProfileString(lpszSection,_T("size"),_T("9pt"),str.GetBuffer(MAX_PATH),MAX_PATH,strCfgPath); str.ReleaseBuffer();//str="12pt" TCHARbuf[200]={0}; intnSize=sizeof(buf)/sizeof(buf[0]); GetPrivateProfileString(lpszSection,NULL,_T(""),buf,nSize,strCfgPath); //buf:"name\0size\0color\0\0" memset(buf,0,sizeof(buf)); GetPrivateProfileString(NULL,_T("size"),_T(""),buf,nSize,strCfgPath);//没Section,_T("size")没意义了,所以可以写NULL //可以是GetPrivateProfileString(NULL,NULL,_T(""),buf,nSize,strCfgPath); //buf:"Font\0Layout\0Body\0\0" memset(buf,0,sizeof(buf)); GetPrivateProfileSection(lpszSection,buf,nSize,strCfgPath); //buf:"name=宋体\0size=12pt\0color=RGB(255,0,0)\0\0"此时“=”两边不会有空格 memset(buf,0,sizeof(buf)); GetPrivateProfileSectionNames(buf,nSize,strCfgPath);//等于GetPrivateProfileString(NULL,NULL,_T(""),buf,nSize,strCfgPath); //buf:"Font\0Layout\0Body\0\0"
写ini文件
WritePrivateProfileString函数,没有写integer的,可以转成string再写入。
BOOLWritePrivateProfileString( LPCTSTRlpAppName,//sectionname LPCTSTRlpKeyName,//keyname LPCTSTRlpString,//stringtoadd LPCTSTRlpFileName//initializationfile ); TheWritePrivateProfileSectionfunctionreplacesthekeysandvaluesforthespecifiedsectioninaninitializationfile. BOOLWritePrivateProfileSection( LPCTSTRlpAppName,//sectionname LPCTSTRlpString,//data LPCTSTRlpFileName//filename );
WritePrivateProfileString:
Remarks
IfthelpFileNameparameterdoesnotcontainafullpathandfilenameforthefile,WritePrivateProfileStringsearchestheWindowsdirectoryforthefile.Ifthefiledoesnotexist,thisfunctioncreatesthefileintheWindowsdirectory.
IflpFileNamecontainsafullpathandfilenameandthefiledoesnotexist,WritePrivateProfileStringcreatesthefile.Thespecifieddirectorymustalreadyexist.
WritePrivateProfileSection:
Remarks
ThedatainthebufferpointedtobythelpStringparameterconsistsofoneormorenull-terminatedstrings,followedbyafinalnullcharacter.Eachstringhasthefollowingform:
key=string
TheWritePrivateProfileSectionfunctionisnotcase-sensitive;thestringpointedtobythelpAppNameparametercanbeacombinationofuppercaseandlowercaseletters.
IfnosectionnamematchesthestringpointedtobythelpAppNameparameter,WritePrivateProfileSectioncreatesthesectionattheendofthespecifiedinitializationfileandinitializesthenewsectionwiththespecifiedkeynameandvaluepairs.
WritePrivateProfileSectiondeletestheexistingkeysandvaluesforthenamedsectionandinsertsthekeynamesandvaluesinthebufferpointedtobythelpStringparameter.Thefunctiondoesnotattempttocorrelateoldandnewkeynames;ifthenewnamesappearinadifferentorderfromtheoldnames,anycommentsassociatedwithpreexistingkeysandvaluesintheinitializationfilewillprobablybeassociatedwithincorrectkeysandvalues.
Thisoperationisatomic;nooperationsthatreadfromorwritetothespecifiedinitializationfileareallowedwhiletheinformationisbeingwritten.
例子:
WritePrivateProfileString(_T("Layout"),_T("left"),_T("100"),strCfgPath); WritePrivateProfileString(_T("Layout"),_T("top"),_T("80"),strCfgPath); //删除某Section,包括[Layout]和其下所有Keys=Value WritePrivateProfileSection(_T("Layout"),NULL,strCfgPath); //删除某Section,包括[Layout]下所有Keys=Value,但不删除[Layout] WritePrivateProfileSection(_T("Layout"),_T(""),strCfgPath); //而:WritePrivateProfileSection(NULL,NULL,strCfgPath);什么也不做,因Section为NULL
自己封装的函数:
获取某一个Section的所有key=value
map
获取ini文件的所有Section名
vector
#include#include
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。