有没有更好的方法让我从文件中读取值并相应地设置它们?我可以以某种方式从文件中读取变量名并在程序中相应地设置它吗?例如,读取 BitDepth 并将其设置为文件中所述的值?因此,我不必检查这条线是否是 BitDepth 然后将 bit_depth 设置为后面的值?
std::ifstream config (exec_path + "/Data/config.ini");
if (!config.good ()) SetStatus (EXIT_FAILURE);
while (!config.eof ()) {
std::string tmp;
std::getline (config, tmp);
if (tmp.find ("=") != 1) {
if (!tmp.substr (0, tmp.find (" ")).compare ("BitDepth")) {
tmp = tmp.substr (tmp.find_last_of (" ") + 1, tmp.length ());
bit_depth = atoi (tmp.c_str ());
} else if (!tmp.substr (0, tmp.find (" ")).compare ("WindowWidth")) {
tmp = tmp.substr (tmp.find_last_of (" ") + 1, tmp.length ());
screen_width = atoi (tmp.c_str ());
} else if (!tmp.substr (0, tmp.find (" ")).compare ("WindowHeight")) {
tmp = tmp.substr (tmp.find_last_of (" ") + 1, tmp.length ());
screen_height = atoi (tmp.c_str ());
}
}
}
配置文件
[Display]
BitDepth = 32
WindowWidth = 853
WindowHeight = 480