【原创】读取INI中所有节点的值

时间:2021-09-06 04:24:57

 TCHAR szSection[1025]={0};
 DWORD dwSection = GetPrivateProfileString(NULL, NULL, NULL, szSection, 1024, strFileName.c_str());

 LPTSTR lpSection = szSection;
 while(*lpSection)
 {
  TCHAR szEntry[1025]={0};
  DWORD dwEntry = GetPrivateProfileString(lpSection, NULL, NULL, szEntry, 1024, strFileName.c_str());
  LPTSTR lpEntry = szEntry;

  while(*lpEntry)
  {
   TCHAR szVal[1204]={0};
   DWORD dwVal = GetPrivateProfileString(lpSection, lpEntry, NULL, szVal, 1024, strFileName.c_str());

   TRACE(_T("[%s](%s) = %s\n"), lpSection, lpEntry, szVal);

   lpEntry += _tcslen(lpEntry) + 1;
  }

  lpSection += _tcslen(lpSection) + 1;
 }

注意:如果有两个节点名称相同,GetPrivateProfileString只能读取第一个节点的内容

例如:

[123]

a=1

b=2

[123]

a=12

b=13

[2]

a=1

b=78

输出结果为:

[123](a) = 1

[123](b) = 2

[123](a) = 1

[123](b) = 2

[2](a) = 1

[2](b) = 78