Boost.PropertyTreeライブラリを使用して、INIファイルの読み取りと書き込みを行うことができます。
INIファイルを読み取り、変更して保存するサンプルコードは次のとおりです。
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
/*
[owner]
name=John Doe
organization=Acme Widgets Inc.
*/
int main(int ac, char* av[])
{
boost::property_tree::ptree pt;
// Read
read_ini("data.ini", pt);
std::cout << pt.get<std::string>("owner.name") << std::endl; // John Doe
std::cout << pt.get<std::string>("owner.organization") << std::endl; // Acme Widgets Inc.
// Write
pt.put<std::string>("owner.name", "New Name");
pt.put<std::string>("owner.name", "New Organization");
write_ini("date2.ini", pt);
return 0;
}
注: Windows CEでこのコードをコンパイルする場合、Boost.PropertyTreeライブラリを調整する必要がある場合があります。WindowsCE用の標準C++ライブラリには<locale>
ヘッダーがなく、Boost.PropertyTreeはINIファイルの読み取りと書き込みの際にヘッダーを使用しているようです。