【问题标题】:Selecting and processing INI file sections that start with a given string [closed]选择和处理以给定字符串开头的 INI 文件部分 [关闭]
【发布时间】:2021-10-11 09:54:25
【问题描述】:

我正在使用内置函数 ReadSections() 从 Ini 文件中读取部分。所以为此我必须将部分名称作为参数传递,但现在我想读取从 ADD.EXE 开始的所有部分,我该怎么做?会有多个部分,如 ADD.EXE_NEW、ADD.EXE_OLD、ADD.EXE_First、SEC.EXE、THIRD.EXE,所以我只想阅读名称以 ADD.EXE 开头的那些部分

  unique_ptr<TDataIniFile> UserIni(new TDataIniFile(File));

  if (UserIni->ReadSections("ADD.EXE")

【问题讨论】:

  • "IniFile" 不是 c++ 标准的一部分。请使用该类的链接(或该类的实际定义)来编辑您的问题。
  • 不要关注 IniFile,它只是类名。尝试理解问题。我面临的主要问题是在下面的行中 if (UserIni->ReadSections("ADD.EXE") 我想要从“ADD.EXE”开始的所有部分我知道在Unix中我们有简单的*星号附加任何内容我们可以很容易地找到,但在 C++ 中我不知道该怎么做。如果你知道,请告诉我们。
  • 这个问题只能通过检查该 IniFile(或显然是 TDataIniFile)类的接口来回答。这就是为什么我要求查看完整的班级声明。如果有类似“GetAllSections”或所有部分的迭代器,您可以使用简单的循环和字符串匹配。
  • 好的,但是没有像 GetAllSections 这样的函数,是的,我们可以使用 ReadSections(),其中可以使用普通循环和字符串匹配。你能告诉我我们该怎么做吗?
  • 第三次了,我不能在不知道实际 API 的情况下这样做,所以我知道 GetAllSections 返回什么!

标签: c++ c++builder


【解决方案1】:

ReadSections()TStrings* 指针指向您创建的字符串列表对象,然后它将使用 INI 中的部分名称填充,例如:

std::unique_ptr<TStringList> sections(new TStringList);
std::unique_ptr<TDataIniFile> UserIni(new TDataIniFile(File));
UserIni->ReadSections(sections.get());

然后您可以根据需要枚举该字符串列表,例如:

for(int i = 0; i < sections->Count; ++i)
{
    String section = sections->Strings[i];
    if (StartsText(_D("ADD.EXE"), section))
    {
        // read values for section from UserIni as needed...
    }
}

或者:

// standard range-for doesn't work with TStringList, but
// std::for_each() does via iterators from non-member
// begin()/end() functions found via ADL...

//for(String section : *sections)
std::for_each(begin(sections.get()), end(sections.get()),
    [&](const String &section)
    {
        if (StartsText(_D("ADD.EXE"), section))
        {
            // read values for section from UserIni as needed...
        }
    }
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多