读写ini文件

读取

3 是默认值
int extensions = GetPrivateProfileInt("sectionName","key",3,g_szIniFileName);

CString tmp,str;
#define MAX_LENGTH 216
GetPrivateProfileString("sectionName",key,NULL,str.GetBuffer(MAX_LENGTH),MAX_LENGTH,g_szIniFileName);
...处理代码
str.ReleaseBuffer(MAX_LENGTH);

写入

WritePrivateProfileString("sectionName","key","value",g_szIniFileName);

遍历

// 获取所有 section 内容
const int nBufferSize = 5120;
TCHAR lpszReturnBuffer[nBufferSize] = { '\0' };
DWORD dwRet = GetPrivateProfileSectionNames(lpszReturnBuffer, nBufferSize, path.c_str());
if (0==dwRet)
{
return -1;
}
m_listSections.clear();
list<wstring> listKVs;
TCHAR* pNextSection = NULL;
pNextSection = lpszReturnBuffer;
do
{
if (*pNextSection != 0x00)
{
m_listSections.push_back(pNextSection);
// 获取 section 下面所有key:value对
listKVs = getSectionData(pNextSection);
Json::Value jsonTmp;
for (auto&it:listKVs)
{
vector<wstring> wv = strHelper::strSplit(it, L"=");
if (wv.size() == 2)
{
jsonTmp[strHelper::w2s(wv[0]).c_str()] = strHelper::w2s(wv[1]);
}
else
{
g_logger->error("CIniTools::loadIni: kv parse failed. {}", strHelper::w2s(it));
}
}
m_json[strHelper::w2s(pNextSection)] = jsonTmp;
}
pNextSection = pNextSection + lstrlen(pNextSection) + 1;
} while (*pNextSection != 0x00);