C++ INI文件的遍历

看别人的博客改动来的
ini文件的遍历

vector<vector<CString>> CLidarSave::TraverseIni(CString m_szPath)
{
	TCHAR returnBuff[2048];
	TCHAR returnKey[MAX_PATH] = { 0 };
	TCHAR szKeyName[MAX_PATH] = { 0 };
	TCHAR szKeyValue[MAX_PATH] = { 0 };

	LPTSTR pTemp;
	LPTSTR pKeyTemp;

	CString tempszKeyName;
	CString tempszKeyValue;
	vector<CString> tempKeyNamelist;
	vector<CString> tempKeyValuelist;
	vector<vector<CString>> returnlist;

	GetPrivateProfileSectionNames(returnBuff, 2048, m_szPath);//获取节名

	pTemp = returnBuff;

	while (*pTemp != 0)
	{
		//SetDlgItemText(IDC_EDIT1, pTemp);

		memset(returnKey, 0, sizeof(returnKey));
		GetPrivateProfileSection(pTemp, returnKey, MAX_PATH, m_szPath);//获取键名键值

		pKeyTemp = returnKey;
		int i = 0;

		while (*pKeyTemp != 0)
		{
			int npos = wcscspn(pKeyTemp, L"=");
			memset(szKeyName, 0, sizeof(szKeyName));
			memset(szKeyValue, 0, sizeof(szKeyValue));

			wcsncpy_s(szKeyName, pKeyTemp, npos);
			wcsncpy_s(szKeyValue, pKeyTemp + npos + 1, wcslen(pKeyTemp) - npos - 1);
			
			tempszKeyName.Format(_T("%s"),szKeyName);
			tempszKeyValue.Format(_T("%s"), szKeyValue);

			tempKeyNamelist.push_back(tempszKeyName);
			tempKeyValuelist.push_back(tempszKeyValue);

			pKeyTemp = wcsrchr(pKeyTemp, 0) + 1;
			i++;
		}

		pTemp = wcsrchr(pTemp, 0) + 1;
	}

	returnlist.push_back(tempKeyNamelist);
	returnlist.push_back(tempKeyValuelist);

	return returnlist;
}

你可能感兴趣的:(C++技巧总结)