【问题标题】:How to read an INI file entirley?如何完全读取 INI 文件?
【发布时间】:2019-04-07 15:19:28
【问题描述】:

我想完全读取 INI 文件,我正在使用 wxFileConfig 类来执行此操作,但互联网的大多数示例只是读取和写入一个项目,而不是整个 INI 文件。

INI文件中的数据类似如下:

[sdl]
fullresolution=0x0
fullscreen=true
output=opengl
autolock=false

[dosbox]
machine=svga_s3
memsize=16

[render]
frameskip=0
aspect=false
scaler=normal2x

[cpu]
core=normal
cputype=auto
cycles=10000
cycleup=1000
cycledown=1000
.....

我试图做某事,但它只是读取标题([sdl]、[dosbox]、[render]、...)。

wxFileConfig config(wxEmptyString, wxEmptyString, wxEmptyString, wxGetCwd() + "\\dosbox.conf");
wxString str;
long idx;
bool bCont = config.GetFirstGroup(str, idx);
while (bCont) {
    bCont = config.GetNextGroup(str, idx);
    debugMsg("%s", str);
}

如何读取每个标题及其项目?

【问题讨论】:

  • 您是要获取单个标题的条目还是要分别读取所有标题和项目?
  • @DeveloperPaul:阅读所有标题和项目。

标签: c++ wxwidgets


【解决方案1】:

取自documentation,您可以像这样阅读所有条目:

// enumeration variables
wxString str;
long dummy;

// first enum all entries
bool bCont = config->GetFirstEntry(str, dummy);
while ( bCont ) {
    aNames.Add(str);
    bCont = config->GetNextEntry(str, dummy);
}

这与你必须阅读所有组的代码非常相似。

【讨论】:

  • 不幸的是,我之前看过该文档并使用过该代码,但没有得到任何项目。
  • @LionKing,你能通过代码看看它在哪里失败了吗?另外,您使用的是什么版本的 wx,您的操作系统是什么?
  • 谢谢,但是您的代码是完整代码的一部分,我在问题中提供的代码也是一部分,但是我在新答案中编写的完整代码并且效果很好。
【解决方案2】:

我找到了一个完整的代码,它可以从 .ini 文件中获取所有数据:

wxFileConfig config(wxEmptyString, wxEmptyString, wxEmptyString, wxGetCwd() + "\\dosbox.conf");
wxString group;
long group_index;

config.SetPath("/");
bool has_group = config.GetFirstGroup(group, group_index);
while (has_group) {
    config.SetPath(group);

    wxString entry;
    long entry_index;

    bool has_entry = config.GetFirstEntry(entry, entry_index);
    while (has_entry) {
        wxString value = config.Read(entry, "");
        wxMessageOutputDebug d;
        d.Printf("[%s] %s = %s", group, entry, value);

        has_entry = config.GetNextEntry(entry, entry_index);
    }

    config.SetPath("/");
    has_group = config.GetNextGroup(group, group_index);
}

The source.

【讨论】:

    猜你喜欢
    • 2011-07-13
    • 2015-10-01
    • 1970-01-01
    • 2013-04-18
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多