我的任务是微不足道的 - 我只需要解析这样的文件:在C++上迭代ini文件,可能使用boost :: property_tree :: ptree?
Apple = 1
Orange = 2
XYZ = 3950
但我不知道一组可用密钥。我解析这个文件中使用C#相对容易,让我演示源代码:
public static Dictionary<string, string> ReadParametersFromFile(string path)
{
string[] linesDirty = File.ReadAllLines(path);
string[] lines = linesDirty.Where(
str => !String.IsNullOrWhiteSpace(str) && !str.StartsWith("//")).ToArray();
var dict = lines.Select(s => s.Split(new char[] { '=' }))
.ToDictionary(s => s[0].Trim(), s => s[1].Trim());
return dict;
}
现在我只需要用C++做同样的事情。我正在考虑使用boost::property_tree::ptree
,但似乎我只是无法遍历ini文件。这很容易读取ini文件:
boost::property_tree::ptree pt;
boost::property_tree::ini_parser::read_ini(path, pt);
但它是不可能遍历它,请参阅这个问题Boost program options - get all entries in section
的问题是 - 什么是上面写上的C#代码模拟的最简单方法C++?
_Of course_有可能迭代ptree,实际上它是微不足道的,因为我的答案现在显示(更新)(注意:程序选项!=属性树) – sehe 2013-04-21 20:13:18