如何从INI文件读取配置文件条目 [英] How to read config file entries from an INI file

查看:131
本文介绍了如何从INI文件读取配置文件条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能使用 Get * Profile 函数,因为我使用的是旧版本的Windows CE平台SDK没有那些。

I can't use the Get*Profile functions because I'm using an older version of the windows CE platform SDK which doesn't have those. It doesn't have to be too general.

[section]
  name = some string

我只需要打开文件,检查是否存在section,以及与name 。标准C ++偏好。

I just need to open the file, check for the existence of "section", and the the value associated with "name". Standard C++ preferred.

推荐答案

我想出了什么:

std::wifstream file(L"\\Windows\\myini.ini");
if (file)
{
  bool section=false;
  while (!file.eof())
  {
    WCHAR _line[256];
    file.getline(_line, ELEMENTS(_line));
    std::wstringstream lineStm(_line);
    std::wstring &line=lineStm.str();
    if (line.empty()) continue;

    switch (line[0])
    {
      // new header
      case L'[':
      {
        std::wstring header;
        for (size_t i=1; i<line.length(); i++)
        {
          if (line[i]!=L']')
            header.push_back(line[i]);
          else
            break;
        }
        if (header==L"Section")
          section=true;
        else
          section=false;
      }
  break;
      // comments
      case ';':
      case ' ':
      case '#':
      break;
      // var=value
      default:
      {
        if (!section) continue;

        std::wstring name, dummy, value;
        lineStm >> name >> dummy;
        ws(lineStm);
        WCHAR _value[256];
        lineStm.getline(_value, ELEMENTS(_value));
        value=_value;
      }
    }
  }
}

这篇关于如何从INI文件读取配置文件条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆