Ini文件内容如下:
[检测结果]
车辆序号=120208743
无载最大运行速度(km/h)=17.6
...车辆序号,无载最大运行速度(km/h)等键名为不确定项,如何动态的获取所有的键名,然后根据键名获取对应的键值。
我使用:
GetPrivateProfileString("检测结果",NULL,NULL,strAllKey.GetBuffer(1024),1024,_T("D:\\IniTest\\120208743.ini") );
strAllKey只获取了第一个键名,不能获取所有的键名。请高手解答,在线等

解决方案 »

  1.   

    Parameters
    lpAppName 
    The name of the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.lpKeyName 
    The name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.参数设为NULL就是返回把有键名了.
      

  2.   

    就是 lpAppName 使用 NULL
      

  3.   

    使用 GetPrivateProfileSectionNames 吧
      

  4.   

    GetPrivateProfileString(NULL,NULL,NULL,strAllKey.GetBuffer(1024),1024,_T("D:\\IniTest\\120208743.ini") );
    返回结果为:检测结果

    GetPrivateProfileSectionNames(strAllKey.GetBuffer(1024), 1024, _T("D:\\IniTest\\120208743.ini") );
    返回结果也为:检测结果
      

  5.   

    GetPrivateProfileString("检测结果",NULL,NULL,strAllKey.GetBuffer(1024),1024,_T("D:\\IniTest\\120208743.ini") );
    返回结果仅为:第一个键名:车辆序号,我获取了字符串的长度,为8
      

  6.   


      {
        TCHAR szSection[1025]={0};
        DWORD dwSection = GetPrivateProfileString(NULL, NULL, NULL, szSection, 1024, iniFile);    LPTSTR lpSection = szSection;
        while(*lpSection)
        {
          TCHAR szEntry[1025]={0};
          DWORD dwEntry = GetPrivateProfileString(lpSection, NULL, NULL, szEntry, 1024, iniFile);
          LPTSTR lpEntry = szEntry;      while(*lpEntry)
          {
            TCHAR szVal[1204]={0};
            DWORD dwVal = GetPrivateProfileString(lpSection, lpEntry, NULL, szVal, 1024, iniFile);        TRACE(_T("[%s](%s) = %s\n"), lpSection, lpEntry, szVal);        lpEntry += _tcslen(lpEntry) + 1;
          }      lpSection += _tcslen(lpSection) + 1;
        }
      }
      

  7.   

    路过。
    有一个比较笨的方法但如果没有好的办法可以用一下。设立一个结构体用于存储键与键值。然后用你提到的方法获取第一个键值对并存于结构体中。然后删除它 再读取第二个键值对存于结构体。以此类推直到最后一个。最后通过结构体链表恢复INI文件(写入)这样就得到了想要的数据了。 呵呵笨了点但可行。