0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C++でConfigparser的なものを使いたかった。

Posted at

最近C++を勉強し始めました。

PythonのConfigparser的なものは無いかなぁと思って探したところ、Inippを見つけたので使ってみました。

Boostでもptreeを用いてiniファイルを扱えますが、デフォルト値を設定したかったのでこちらを採用しました。

他に便利なものあれば教えて頂けると助かります。

サンプル

config.ini
[DEFAULT]
value1 = number
value2 = 123
value3 = true

[setting1]
value4 = setting1

[setting2]
value4 = setting2
sample.cpp
# include <fstream>
# include <string>

# include "inipp.h"

int main()
{
	inipp::Ini<char> ini;
	std::ifstream is("config.ini");
	ini.parse(is);
	ini.strip_trailing_comments();
	ini.default_section(ini.sections["DEFAULT"]);
	ini.interpolate();

	std::string value1;
	int value2;
	bool value3;

	inipp::get_value(ini.sections["setting1"], "value1", value1);
	inipp::get_value(ini.sections["setting1"], "value2", value2);
	inipp::get_value(ini.sections["setting1"], "value3", value3);

	std::cout << "value1: " << value1 << std::endl;
	std::cout << "value2: " << value2 << std::endl;
	std::cout << "value3: " << value3 << std::endl;
	std::cout << "---------------------" << std::endl;

	std::string value4_setting1;
	std::string value4_setting2;
	inipp::get_value(ini.sections["setting1"], "value4", value4_setting1);
	inipp::get_value(ini.sections["setting2"], "value4", value4_setting2);
	std::cout << "value4_setting1: " << value4_setting1 << std::endl;
	std::cout << "value4_setting2: " << value4_setting2 << std::endl;

	return 0;
}

出力

value1: number
value2: 123
value3: 1
---------------------
value4_setting1: setting1
value4_setting2: setting2
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?