您的位置:首页 > 其它

获取ini文件所有的Sections和Keys

2016-04-29 22:23 295 查看
获取ini文件中所有的Sections和Keys,并以pair对的方式存入到vector中

#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
using namespace std;

#define PATH "E:\\vc_code\\parse_ini\\cfg.ini"
int main()
{
char buff[1024] = {0};
vector<string> vecSections;
vector<string> vecKeys;
vector<pair<string, string>> vecSectionKey;
int num = GetPrivateProfileSectionNames(buff, 1024, PATH);
size_t startpos = 0;
for (size_t i = 0; i < num; ++i)
{
if ('\0' == buff[i])
{
string tmp(buff + startpos, buff + i);
startpos = i + 1;
vecSections.push_back(tmp);
}
}

for (size_t i = 0; i < vecSections.size(); ++i)
{
char buffkey[1024] = {0};
num = GetPrivateProfileSection(vecSections[i].c_str(), buffkey, 1024, PATH);
startpos = 0;
for (size_t j = 0; j < num; ++j)
{
if ('\0' == buffkey[j])
{
string tmp(buffkey + startpos, buffkey + j);
startpos = j + 1;
size_t pos = tmp.find('=');
vecKeys.push_back(tmp.substr(0, pos));

vecSectionKey.push_back(make_pair(vecSections[i], tmp.substr(0, pos)));
}
}
}

return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: