使用GetPrivateProfileSectionNames从ini文件中读取所有节的名称
问题描述:
从ini文件中读取所有节的名称.
当我使用 GetPrivateProfileSectionNames 时,我得到的只是第一部分的名称.如何获得下一个部分的名称?请帮忙
Hi,
Reading all section names from ini file.
I am getting the first section name alone when i use GetPrivateProfileSectionNames. How can i get the next section name ? Please help
// reading each section from ini file
LPWSTR pSecNames= new WCHAR[1024];
int nSectionNum = 0;
wstring strSecName;
DWORD retVal = GetPrivateProfileSectionNames(pSecNames,1024,profileFilePath.c_str());
if (retVal)
{
strSecName.assign(pSecNames,retVal);
}
delete pSecNames;
string sectionName = ToString(strSecName.c_str(),CP_UTF8);
答
来自MSDN :
lpszReturnBuffer [out]
A pointer to a buffer that receives the section names associated with the named file. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character.
因此,如果您的INI文件为:
So, if your INI file is:
[Hello]
a=1
b=2
[World]
c=3
d=4
pSecNames将为"Hello \ 0World \ 0 \ 0",而retVal将为13.
您需要某种循环才能全部读取它们:
pSecNames will be "Hello\0World\0\0", and retVal will be 13.
You''ll need some kind of loop to read them all:
// This is pseudo-code, I didn't compile it.
std::list<std::wstring> l;
for (wchar_t * p = pSecNames; *p; ++p)
{
std::wstring ws(p);
l.push_back(ws);
p += ws.size();
ASSERT(0 == *p);
}
希望这会有所帮助,
Pablo.
Hope this helps,
Pablo.
LPTSTR lpszReturnBuffer;
lpszReturnBuffer = new TCHAR[MAX];
char* pNextSection = NULL;
GetPrivateProfileSectionNames(lpszReturnBuffer,MAX,IniPath);
pNextSection = lpszReturnBuffer;
printf("%s\n", pNextSection);
CString csAllSections;
while (*pNextSection != 0x00)
{
pNextSection = pNextSection + strlen(pNextSection) + 1;
if(*pNextSection != 0x00)
{
csAllSections += pNextSection;
}
}
csAllSections将具有所有节的名称.但没有分开.您必须处理这种逻辑
csAllSections will have all sections name. but not separted. You have to handle that logic