VC枚举配置文件的Section存入Map
来源:互联网 发布:java api 1.8英文版 编辑:程序博客网 时间:2024/06/05 09:41
配置文件横行的开发任务,各种各样的配置文件着实令人心烦,却又无可奈何!
需求:
将配置文件中的某个Section枚举出来存入STL的MAP中,以便后续操作。
实现:
使用GetPrivateProfileSection获取Section下所有值,然后使用字符串处理之后存入MAP中。
函数原型如下(分为Unicode版和非Unicode版本):
GetPrivateProfileSection(
LPCSTR lpAppName, //Section名称
LPSTR lpReturnedString, //存放返回的字符串缓存
DWORD nSize, //缓存大小
LPCSTR lpFileName //配置文件路径
);
返回实际读取的字符数。
注意:GetPrivateProfileSection函数读去的Section以字符串的形式存入lpReturnedString中,键值对之间的空格会被过滤,换行符用‘\0'替换,结尾为'\0\0'.这里容易出问题。
假设配置文件property.ini内容如下:
[Section]
key1=value1
key2=value2
key3=value3
key4=value4
key5=value5
实现如下:
map<int,string>strMap;void StrReplace(string src,string s){string::size_type pos=0;string::size_type start_pos = 0;//根据字符串s分割字符串srcwhile((pos=src.find_first_of(s,pos))!=string::npos){string temp = src.substr(start_pos,pos-start_pos);string::size_type n_pos = 0;//根据'='分割字符串srcwhile((n_pos=temp.find_first_of('=',n_pos)) != string::npos){string key,value;key = temp.substr(0,n_pos);value = temp.substr(n_pos+1,temp.length());n_pos++;strMap.insert(make_pair<int,string>(atoi(key.c_str()),value));}start_pos = (++pos);}}int ReadPaperTypeProperty(){char chSection[1024] = {0};int nSize = GetPrivateProfileSection("Section",chSection,1024,"property.ini");//替换字符串中间的'\0'while (strlen(chSection) < nSize){chSection[strlen(chSection)] = ',';}string strSection = chSection;StrReplace(strSection,",");return 0;}
总结一下GetPrivateProfileSectionNames的用法:
GetPrivateProfileSectionNames的功能是枚举配置文件中所有Section的名称,函数原型如下:
GetPrivateProfileSectionNamesA(
LPSTR lpszReturnBuffer, //存放名称缓冲区
DWORD nSize, //缓冲区大小
LPCSTR lpFileName //配置文件名称
);
注意:读取的Section名同样以字符串的形式存入lpReturnedString中,键值对之间的空格会被过滤,换行符用‘\0'替换,结尾为'\0\0'.这里容易出问题。
int GetSectionNames(){char chSectionNames[1024] = {0};int nSize = GetPrivateProfileSectionNames(chSectionNames,sizeof(chSectionNames),property.ini" );//替换字符串中间的'\0'while (strlen(chSectionNames) < nSize){chSectionNames[strlen(chSectionNames)] = ',';}MessageBox(NULL,chSectionNames,"",MB_OK);return nSize;}
- VC枚举配置文件的Section存入Map
- java的map存入null值
- 写自己的配置文件中的section
- VC程序的SECTION(3):.text
- 用位域枚举作将多个 Boolean 值存入数据库的同一个字段
- JavaSE024_枚举的应用之存入List集合
- vc配置文件的生成
- USACO Section 2.2 Runaround Numbers - 又一个枚举的思想
- USACO Section 4.4 Pollutant Control - 找更优的搜索枚举方案~
- USACO Section 5.2 Snail Trail - 很水的枚举..
- USACO Section 5.2 Electric Fences - 有意思的枚举+计算几何
- 将用逗号隔开的字符串存入MAP里
- 字符拆分存入Map计算单词的个数
- C#中读取配置文件ini中的所有Section 以及制定Section下的所有Keys
- VC配置文件ini的读写
- VC配置文件ini的读写
- VC配置文件ini的读写
- VC配置文件ini的读写
- linux pthread 编程基础
- 电子银行项目总结
- jquery中方法的扩展
- 一招制敌 - 玩转 AngularJS 指令的 Scope (作用域)
- HDU 1722 Cake(思维题)
- VC枚举配置文件的Section存入Map
- 欢迎使用CSDN-markdown编辑器
- 写论文注意事项
- jQuery滑过头像图片展示个人信息效果
- android.app.Activity.startActivity(Intent intent)
- C++ 语法
- Spring:加载外部properties文件
- JS实现下拉列表联动
- 百度地图SDK导航功能初始化错误