我想完全阅读一个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);
}
如何阅读每个标题及其项目?
取自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);
}
它与您必须阅读所有组的代码非常相似。
我找到了一个完整的代码,它带来了.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);
}