Wince读取ini的CIniParse类

//========================================================================
//TITLE:
// Wince读取ini的CIniParse类
//AUTHOR:
// norains
//DATE:
// Monday 22-December-2008
//Environment:
// WINCE5.0 + VS2005 + ARM BSP
//========================================================================

挺奇怪的是,微软在wince中没有像桌面系统般有读取ini的特定函数。为了解决该问题,网上有不少牛人都给给出了相应的解决方案,比如说:

benkaoya的直接C++/C代码读写:http://blog.youkuaiyun.com/benkaoya/archive/2008/01/28/2070648.aspx

xumercury的将以上函数封装为类:http://blog.youkuaiyun.com/xumercury/archive/2008/07/09/2629346.aspx

khan的STL读写类:http://www.cppblog.com/Khan/archive/2007/08/09/29459.html?opt=admin

不过,以上的这几个都有一些小问题,或是效率较低,或是有一些小bug,其实最引起自己重写冲动的是因为命名规则和自己的理念有冲突,所以索性就自己另起炉灶弄了个CIniParse。因为习惯问题,我的代码基本上充斥的是STL代码,如果不喜欢这风格,可以使用benkaoya或xumercury的作品,他们的这个代码也是比较成熟的;关于khan的LIniFile类,在他blog的留言中,似乎公布的这个类代码是有缺陷的,完整的代码需要发信向其索取。因为我没有他的完整版代码,所以我也不做更多的评论。

写完这段有损人利己嫌疑的言语后,我们来看看今天的主角:CIniParse。之前说了别人的代码的缺陷,现在也该说说CIniParse所存在的一些问题了。可能最让人不爽的是,CIniParse类在保存的时候,会将原来的注释全部清除掉,并且原来的顺序也全部重排。如果注释以及顺序对你非常重要,请勿使用CIniParse类。

除此以外,该类并不能在ASCII环境中编译通过。只是wince是UNICODE的系统,所以这点在平时使用中倒不会引起很大的麻烦。当然,如果你是打算移植到VC6.0中,那么可能有一些函数就必须要修正了。再一点就是,该类没有详细地进行测试,所以应该还会有或多或少的问题,如果你发现了,也希望我更正,期待你的指出。

惯例,先罗列出CIniParse的完整代码:

头文件:

  1. //ClassName
  2. //CIniParse
  3. //
  4. //Version:
  5. //1.0.0
  6. //
  7. //Date:
  8. //2008.12.22
  9. //
  10. //Author:
  11. //norains
  12. //
  13. #pragmaonce
  14. #include"windows.h"
  15. #include<string>
  16. #include<vector>
  17. #include<map>
  18. #ifdefUNICODE
  19. #ifndefTSTRING
  20. #defineTSTRINGstd::wstring
  21. #endif
  22. #else
  23. #ifndefTSTRING
  24. #defineTSTRINGstd::string
  25. #endif
  26. #endif
  27. classCIniParse
  28. {
  29. public:
  30. //-------------------------------------------------------------------------------
  31. //Description:
  32. //Opentheinifile.
  33. //-------------------------------------------------------------------------------
  34. BOOLOpen(TSTRINGstrFile);
  35. //-------------------------------------------------------------------------------
  36. //Description:
  37. //Gettheprofilevalueasstringtype
  38. //-------------------------------------------------------------------------------
  39. TSTRINGGetPrivateProfileString(constTSTRING&strSection,constTSTRING&strKey);
  40. //-------------------------------------------------------------------------------
  41. //Description:
  42. //Gettheprofilevalueasinttype
  43. //-------------------------------------------------------------------------------
  44. intGetPrivateProfileInt(constTSTRING&strSection,constTSTRING&strKey);
  45. //-------------------------------------------------------------------------------
  46. //Description:
  47. //Settheprofilevalueasstringtype.Thefunctionwouldn'tsavethedatatofile
  48. //butmemory.Ifyouwanttosavetothefile,youmustcallFlushfunction
  49. //-------------------------------------------------------------------------------
  50. BOOLSetPrivateProfileString(constTSTRING&strSection,constTSTRING&strKey,TSTRINGstrSet);
  51. //-------------------------------------------------------------------------------
  52. //Description:
  53. //Settheprofilevalueasinttype.Thefunctionwouldn'tsavethedatatofile
  54. //butmemory.Ifyouwanttosavetothefile,youmustcallFlushfunction
  55. //-------------------------------------------------------------------------------
  56. BOOLSetPrivateProfileInt(constTSTRING&strSection,constTSTRING&strKey,intiSet);
  57. //-------------------------------------------------------------------------------
  58. //Description:
  59. //Flushthememorybuffertothefile
  60. //-------------------------------------------------------------------------------
  61. BOOLFlush();
  62. public:
  63. CIniParse();
  64. virtual~CIniParse();
  65. private:
  66. //-------------------------------------------------------------------------------
  67. //Description:
  68. //Getthelinevaluebaseonthecurrentoffset.Aftercalling,theoffsetvalue
  69. //wouldmovetotheheadofnextline
  70. //-------------------------------------------------------------------------------
  71. TSTRINGGetLine();
  72. //-------------------------------------------------------------------------------
  73. //Description:
  74. //Checkthestringvalueofonelinewhetheriscommentornot
  75. //-------------------------------------------------------------------------------
  76. staticBOOLIsCommentLine(constTSTRING&strLine);
  77. //-------------------------------------------------------------------------------
  78. //Description:
  79. //Checkthestringvalueofonelinewhetheristhesection.
  80. //Parameters:
  81. //strLine:[in]Thestringvaluebuffer.
  82. //-------------------------------------------------------------------------------
  83. staticBOOLIsSectionLine(constTSTRING&strLine);
  84. //-------------------------------------------------------------------------------
  85. //Description:
  86. //Getthekeyvaluefromoneline
  87. //Parameters:
  88. //strLine:[in]Thebuffertofind
  89. //-------------------------------------------------------------------------------
  90. staticTSTRINGGetKeyValueFromLine(constTSTRING&strLine);
  91. //-------------------------------------------------------------------------------
  92. //Description:
  93. //Getthekeynamefromoneline
  94. //Parameters:
  95. //strLine:[in]Thebuffertofind
  96. //-------------------------------------------------------------------------------
  97. staticTSTRINGGetKeyNameFromLine(constTSTRING&strLine);
  98. //-------------------------------------------------------------------------------
  99. //Description:
  100. //Getthesectionnamefromoneline
  101. //Parameters:
  102. //strLine:[in]Thebuffertofind
  103. //-------------------------------------------------------------------------------
  104. staticTSTRINGGetSectionNameFromLine(constTSTRING&strLine);
  105. //-------------------------------------------------------------------------------
  106. //Description:
  107. //Removethespacefromthestring
  108. //Parameters:
  109. //strBuf:[in]Thebuffertoremove
  110. //ReturnValue:
  111. //Returnthestringwithoutspace
  112. //-------------------------------------------------------------------------------
  113. staticTSTRINGRemoveSpace(constTSTRING&strBuf);
  114. //-------------------------------------------------------------------------------
  115. //Description:
  116. //Parsetheinifile
  117. //-------------------------------------------------------------------------------
  118. voidParse(constTSTRING&strBuf);
  119. //-------------------------------------------------------------------------------
  120. //Description:
  121. //Convertthestringtolowercase
  122. //-------------------------------------------------------------------------------
  123. staticTSTRINGConvertToLowercase(constTSTRING&strBuf);
  124. //-------------------------------------------------------------------------------
  125. //Description:
  126. //Resettheoffsetpointtothebegin
  127. //-------------------------------------------------------------------------------
  128. BOOLResetOffset();
  129. //-------------------------------------------------------------------------------
  130. //Description:
  131. //Checkwhethertheoffsetarrivedtheendofthebuffer.
  132. //-------------------------------------------------------------------------------
  133. BOOLIsOffsetEnd();
  134. private:
  135. TSTRINGm_strFileBuf;
  136. TSTRING::size_typem_stOffset;
  137. TSTRINGm_strFilePath;
  138. std::map<TSTRING,std::map<TSTRING,TSTRING>>m_mpValue;//ThefirstTSTRINGissectionname,thesecondiskeynameandlastisthevalue.
  139. };

实现体:

  1. #include"IniParse.h"
  2. #include<deque>
  3. #include<algorithm>
  4. CIniParse::CIniParse():
  5. m_stOffset(0)
  6. {}
  7. CIniParse::~CIniParse()
  8. {}
  9. BOOLCIniParse::Open(TSTRINGstrFile)
  10. {
  11. HANDLEhFile=CreateFile(strFile.c_str(),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  12. if(hFile==INVALID_HANDLE_VALUE)
  13. {
  14. returnFALSE;
  15. }
  16. m_strFilePath=strFile;
  17. DWORDdwSize=GetFileSize(hFile,NULL);
  18. //Readthefiletobuffer
  19. std::vector<BYTE>vtBuf(dwSize,0);
  20. DWORDdwRead=0;
  21. ReadFile(hFile,&vtBuf[0],vtBuf.size(),&dwRead,NULL);
  22. vtBuf.resize(dwRead);
  23. CloseHandle(hFile);
  24. #ifdefUNICODE
  25. m_strFileBuf.clear();
  26. if(vtBuf.size()>=2)
  27. {
  28. if(vtBuf[0]==0xFF&&vtBuf[1]==0xFE)
  29. {
  30. //Unicodefile
  31. //Convertthereadbuffertotheunicode.TheTCHARhereisequaltowchar_t
  32. std::deque<TCHAR>dqBuf;
  33. for(std::vector<BYTE>::size_typei=2;i<vtBuf.size();i+=2)
  34. {
  35. dqBuf.push_back(vtBuf[i]+(vtBuf[i+1]<<8));
  36. }
  37. m_strFileBuf.insert(m_strFileBuf.end(),dqBuf.begin(),dqBuf.end());
  38. }
  39. else
  40. {
  41. std::vector<TCHAR>vtBufT;
  42. vtBufT.resize(MultiByteToWideChar(CP_ACP,0,reinterpret_cast<char*>(&vtBuf[0]),vtBuf.size(),NULL,0));
  43. MultiByteToWideChar(CP_ACP,0,reinterpret_cast<char*>(&vtBuf[0]),vtBuf.size(),&vtBufT[0],vtBufT.size());
  44. m_strFileBuf.insert(m_strFileBuf.end(),vtBufT.begin(),vtBufT.end());
  45. }
  46. }
  47. #else
  48. #error"Unfortunately!Thesourcecodedonesn'tcompleteintheASCIIenvironment"
  49. #endif
  50. Parse(m_strFileBuf);
  51. returnTRUE;
  52. }
  53. TSTRINGCIniParse::GetLine()
  54. {
  55. TSTRINGstrReturn;
  56. if(m_stOffset>=m_strFileBuf.size())
  57. {
  58. returnTSTRING();
  59. }
  60. TSTRING::size_typestPos=m_strFileBuf.find(TEXT("/r/n"),m_stOffset);
  61. if(stPos==TSTRING::npos)
  62. {
  63. strReturn.insert(0,m_strFileBuf,m_stOffset,m_strFileBuf.size()-m_stOffset+1);
  64. //Movetheoffsetpositiontoendofthefile
  65. m_stOffset=m_strFileBuf.size()+1;
  66. }
  67. else
  68. {
  69. strReturn.insert(0,m_strFileBuf,m_stOffset,stPos-m_stOffset);
  70. //Movetheoffsetpositiontothebackofthe"/r/n"
  71. m_stOffset=stPos+2;
  72. }
  73. returnstrReturn;
  74. }
  75. BOOLCIniParse::IsCommentLine(constTSTRING&strLine)
  76. {
  77. if(strLine.empty()!=FALSE||strLine[0]==';')
  78. {
  79. returnTRUE;
  80. }
  81. else
  82. {
  83. returnFALSE;
  84. }
  85. }
  86. BOOLCIniParse::IsSectionLine(constTSTRING&strLine)
  87. {
  88. TSTRING::size_typestLeft=strLine.find(TEXT("["));
  89. TSTRING::size_typestRight=strLine.find(TEXT("]"));
  90. if(strLine.empty()==FALSE&&stLeft!=TSTRING::npos&&stRight!=TSTRING::npos&&stRight>stLeft)
  91. {
  92. returnTRUE;
  93. }
  94. else
  95. {
  96. returnFALSE;
  97. }
  98. }
  99. TSTRINGCIniParse::GetKeyValueFromLine(constTSTRING&strLine)
  100. {
  101. TSTRING::size_typestPosEqual=strLine.find(TEXT("="));
  102. if(stPosEqual==TSTRING::npos)
  103. {
  104. returnTSTRING();
  105. }
  106. TSTRINGstrReturn;
  107. TSTRING::size_typestPosQuoteLeft=strLine.find(TEXT("/""),stPosEqual);
  108. if(stPosQuoteLeft!=TSTRING::npos)
  109. {
  110. TSTRING::size_typestPosQuoteRight=strLine.find(TEXT("/""),stPosQuoteLeft+1);
  111. if(stPosQuoteRight!=TSTRING::npos&&stPosQuoteLeft+1!=stPosQuoteRight)
  112. {
  113. strReturn.insert(0,strLine,stPosQuoteLeft+1,stPosQuoteRight-stPosQuoteLeft-1);
  114. returnstrReturn;
  115. }
  116. }
  117. //Storethestringtothebuffer
  118. if(stPosEqual+1>strLine.size())
  119. {
  120. returnTSTRING();
  121. }
  122. TSTRINGstrBuf(strLine,stPosEqual+1,strLine.size()-stPosEqual);;
  123. returnRemoveSpace(strBuf);
  124. }
  125. TSTRINGCIniParse::GetKeyNameFromLine(constTSTRING&strLine)
  126. {
  127. TSTRING::size_typestPosEqual=strLine.find(TEXT("="));
  128. if(stPosEqual==0)
  129. {
  130. returnTSTRING();
  131. }
  132. TSTRINGstrBuf;
  133. if(stPosEqual==TSTRING::npos)
  134. {
  135. //Allofthestringlineisthekeyname
  136. strBuf=strLine;
  137. }
  138. else
  139. {
  140. strBuf.clear();
  141. strBuf.insert(0,strLine,0,stPosEqual);
  142. }
  143. returnRemoveSpace(strBuf);
  144. }
  145. TSTRINGCIniParse::GetSectionNameFromLine(constTSTRING&strLine)
  146. {
  147. TSTRING::size_typestLeft=strLine.find(TEXT("["));
  148. TSTRING::size_typestRight=strLine.find(TEXT("]"));
  149. if(!(strLine.empty()==FALSE&&stLeft!=TSTRING::npos&&stRight!=TSTRING::npos&&stRight>stLeft))
  150. {
  151. returnTSTRING();
  152. }
  153. TSTRINGstrBuf(strLine,stLeft+1,stRight-stLeft-1);
  154. returnRemoveSpace(strBuf);
  155. }
  156. TSTRINGCIniParse::RemoveSpace(constTSTRING&strBuf)
  157. {
  158. if(strBuf.find(TEXT(""))!=TSTRING::npos)
  159. {
  160. //Removethespace
  161. TSTRINGstrReturn;
  162. for(TSTRING::size_typestPos=0;stPos<strBuf.size();stPos++)
  163. {
  164. if(strBuf[stPos]!='')
  165. {
  166. strReturn.push_back(strBuf[stPos]);
  167. }
  168. }
  169. returnstrReturn;
  170. }
  171. else
  172. {
  173. //Nospace
  174. returnstrBuf;
  175. }
  176. }
  177. voidCIniParse::Parse(constTSTRING&strBuf)
  178. {
  179. //Resetthefilepointertothebegin
  180. ResetOffset();
  181. std::map<TSTRING,TSTRING>mpKey;
  182. TSTRINGstrSection;
  183. while(TRUE)
  184. {
  185. TSTRINGstrLine=GetLine();
  186. if(strLine.empty()!=FALSE||IsCommentLine(strLine)!=FALSE)
  187. {
  188. if(IsOffsetEnd())
  189. {
  190. break;
  191. }
  192. else
  193. {
  194. continue;
  195. }
  196. }
  197. if(IsSectionLine(strLine)!=FALSE)
  198. {
  199. if(strSection.empty()!=FALSE)
  200. {
  201. //It'sthefirstsection
  202. strSection=GetSectionNameFromLine(strLine);
  203. continue;
  204. }
  205. //Storethelastsectionvalue
  206. m_mpValue.insert(std::make_pair(strSection,mpKey));
  207. strSection=GetSectionNameFromLine(strLine);
  208. mpKey.clear();
  209. }
  210. else
  211. {
  212. if(strSection.empty()!=FALSE)
  213. {
  214. //Thesectionnameisempty,soneedn'tstorethekeyvalue
  215. continue;
  216. }
  217. //Storethekeyvalueandname
  218. TSTRINGstrKeyName=GetKeyNameFromLine(strLine);
  219. if(strKeyName.empty()==FALSE)
  220. {
  221. mpKey.insert(std::make_pair(strKeyName,GetKeyValueFromLine(strLine)));
  222. }
  223. }
  224. }
  225. //Storethelastsectionvalue
  226. if(strSection.empty()==FALSE)
  227. {
  228. m_mpValue.insert(std::make_pair(strSection,mpKey));
  229. }
  230. }
  231. TSTRINGCIniParse::ConvertToLowercase(constTSTRING&strBuf)
  232. {
  233. std::vector<TCHAR>vtBuf(strBuf.length()+1,0);
  234. _tcscpy(&vtBuf[0],strBuf.c_str());
  235. return_tcslwr(&vtBuf[0]);
  236. }
  237. BOOLCIniParse::ResetOffset()
  238. {
  239. m_stOffset=0;
  240. returnTRUE;
  241. }
  242. BOOLCIniParse::IsOffsetEnd()
  243. {
  244. if(m_stOffset>=m_strFileBuf.size())
  245. {
  246. returnTRUE;
  247. }
  248. else
  249. {
  250. returnFALSE;
  251. }
  252. }
  253. TSTRINGCIniParse::GetPrivateProfileString(constTSTRING&strSection,constTSTRING&strKey)
  254. {
  255. if(m_mpValue.empty()!=FALSE)
  256. {
  257. returnTSTRING();
  258. }
  259. //Ignoringthecharactercasetofindthespecifiedkey
  260. for(std::map<TSTRING,std::map<TSTRING,TSTRING>>::iteratoriterSection=m_mpValue.begin();iterSection!=m_mpValue.end();iterSection++)
  261. {
  262. if(ConvertToLowercase(iterSection->first)==ConvertToLowercase(strSection))
  263. {
  264. for(std::map<TSTRING,TSTRING>::iteratoriterKey=iterSection->second.begin();iterKey!=iterSection->second.end();iterKey++)
  265. {
  266. if(ConvertToLowercase(iterKey->first)==ConvertToLowercase(strKey))
  267. {
  268. returniterKey->second;
  269. }
  270. }
  271. }
  272. }
  273. returnTSTRING();
  274. }
  275. intCIniParse::GetPrivateProfileInt(constTSTRING&strSection,constTSTRING&strKey)
  276. {
  277. return_ttoi(GetPrivateProfileString(strSection,strKey).c_str());
  278. }
  279. BOOLCIniParse::SetPrivateProfileString(constTSTRING&strSection,constTSTRING&strKey,TSTRINGstrSet)
  280. {
  281. //Ignoringthecharactercasetofindthespecifiedkey
  282. for(std::map<TSTRING,std::map<TSTRING,TSTRING>>::iteratoriterSection=m_mpValue.begin();iterSection!=m_mpValue.end();iterSection++)
  283. {
  284. if(ConvertToLowercase(iterSection->first)==ConvertToLowercase(strSection))
  285. {
  286. for(std::map<TSTRING,TSTRING>::iteratoriterKey=iterSection->second.begin();iterKey!=iterSection->second.end();iterKey++)
  287. {
  288. if(ConvertToLowercase(iterKey->first)==ConvertToLowercase(strKey))
  289. {
  290. iterKey->second=strSet;
  291. returnTRUE;
  292. }
  293. }
  294. //Addthenewkeyvalue
  295. iterSection->second.insert(std::make_pair(strKey,strSet));
  296. returnTRUE;
  297. }
  298. }
  299. //Addthenewsectionandkeyvalue
  300. std::map<TSTRING,TSTRING>mpKey;
  301. mpKey.insert(std::make_pair(strKey,strSet));
  302. m_mpValue.insert(std::make_pair(strSection,mpKey));
  303. returnTRUE;
  304. }
  305. BOOLCIniParse::SetPrivateProfileInt(constTSTRING&strSection,constTSTRING&strKey,intiSet)
  306. {
  307. std::vector<TCHAR>vtBuf(MAX_PATH,0);
  308. TSTRINGstrSet=_itot(iSet,&vtBuf[0],10);
  309. returnSetPrivateProfileString(strSection,strKey,strSet);
  310. }
  311. BOOLCIniParse::Flush()
  312. {
  313. TSTRINGstrWrite;
  314. //strWrite.reserve(m_mpValue.size());
  315. //Storethestringvaluetothebuffer
  316. for(std::map<TSTRING,std::map<TSTRING,TSTRING>>::iteratoriterSection=m_mpValue.begin();iterSection!=m_mpValue.end();iterSection++)
  317. {
  318. strWrite+=TEXT("/r/n[");
  319. strWrite+=iterSection->first;
  320. strWrite+=TEXT("]/r/n");
  321. for(std::map<TSTRING,TSTRING>::iteratoriterKey=iterSection->second.begin();iterKey!=iterSection->second.end();iterKey++)
  322. {
  323. strWrite+=iterKey->first;
  324. strWrite+=TEXT("=");
  325. strWrite+=iterKey->second;
  326. strWrite+=TEXT("/r/n");
  327. }
  328. }
  329. //Writetothefile
  330. HANDLEhFile=CreateFile(m_strFilePath.c_str(),GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  331. if(INVALID_HANDLE_VALUE!=hFile)
  332. {
  333. std::vector<BYTE>vtWrite;
  334. vtWrite.reserve(sizeof(TCHAR)*strWrite.size());
  335. #ifdefUNICODE
  336. vtWrite.push_back(0xFF);
  337. vtWrite.push_back(0xFE);
  338. for(TSTRING::iteratoriter=strWrite.begin();iter!=strWrite.end();iter++)
  339. {
  340. vtWrite.push_back(static_cast<BYTE>(*iter));
  341. vtWrite.push_back((*iter)>>8);
  342. }
  343. #else
  344. vtWrite.assign(strWrite.begin(),strWrite.end());
  345. #endif
  346. DWORDdwWrite=0;
  347. WriteFile(hFile,&vtWrite[0],vtWrite.size(),&dwWrite,NULL);
  348. CloseHandle(hFile);
  349. returnTRUE;
  350. }
  351. else
  352. {
  353. returnFALSE;
  354. }
  355. }

具体使用如下:

  1. intWINAPIWinMain(HINSTANCEhInstance,
  2. HINSTANCEhPrevInstance,
  3. LPTSTRlpCmdLine,
  4. intnCmdShow)
  5. {
  6. //声明一个对象
  7. CIniParseiniParse;
  8. //打开相应的ini文件
  9. iniParse.Open(TEXT("//NAND//test.ini"));
  10. TSTRINGstrValue;
  11. intiValue=0;
  12. //获取特定的SECTION和KEY的数值。可以有两种返回形式,一种是TSTRING,另一种是int。
  13. strValue=iniParse.GetPrivateProfileString(TEXT("VERSION_INI_FILE"),TEXT("VERSION_CONFIG_INFO"));
  14. iValue=iniParse.GetPrivateProfileInt(TEXT("VERSION_INI_FILE"),TEXT("VERSION_CONFIG_INFO"));
  15. //更改相应KEY的数值
  16. iniParse.SetPrivateProfileString(TEXT("VERSION_INI_FILE"),TEXT("VERSION_CONFIG_INFO"),TEXT("5600"));
  17. strValue=iniParse.GetPrivateProfileString(TEXT("VERSION_INI_FILE"),TEXT("VERSION_CONFIG_INFO"));
  18. //增加新的SECTION和KEY数值
  19. iniParse.SetPrivateProfileString(TEXT("VERSION_INI_FILE_NEW"),TEXT("VERSION_CONFIG_INFO_NEW"),TEXT("98600"));
  20. strValue=iniParse.GetPrivateProfileString(TEXT("VERSION_INI_FILE_NEW"),TEXT("VERSION_CONFIG_INFO_NEW"));
  21. //写到文件中
  22. iniParse.Flush();
  23. return0;
  24. }

在这里还有一点需要注意的是,因为从效率考虑,SetPrivateProfileString函数更改的数值都只是在内存中做修改,如果需要保存到文件中,需要调用Flush函数。调用Flush函数后,内存的数据就保存到之前Open传入的文件路径中。

如果想保存到C++动态数组中,也可以实现,只是有点麻烦:

  1. strValue=iniParse.GetPrivateProfileString(TEXT("VERSION_INI_FILE_NEW"),TEXT("VERSION_CONFIG_INFO_NEW"));
  2. TCHAR*pNewBuf=newTCHAR[strValue.size()+1];
  3. _tcscpy(pNewBuf,strValue.c_str());
  4. delete[]pNewBuf;

用动态数组还需要手动释放资源,相对来说,就不如直接用STL来得更为简便。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值