我正在使用toml++。如何使用toml++修改CPP中TOML文件的特定值
json ConfigService::getAllData(std::string filePath)
{
try
{
auto _data_table = toml::parse_file(filePath);
_data_table["config_paths"]["DIAAI_core"] = "diaaiSetting.toml"; # this is not working
cout << _data_table << endl;
}
catch (const exception err)
{
std::cerr << "Parsing failed:\n"
<< err.what() << "\n";
std::runtime_error(err.what());
}
}回溯
error: no match for ‘operator=’ (operand types are ‘toml::v3::node_view<toml::v3::node>’ and ‘int’)
51 | _data_table["config_paths"] = 1;发布于 2022-07-18 20:57:27
有几天,我也遇到了这个问题,并研究了许多使用toml++设置值的方法。我想出的是一个暂时的解决方案,但效果很好。
#include <iostream>
#include <toml++/toml.hpp>
using namespace std::string_view_literals;
int main() {
static constexpr auto source = R"(
[config_paths]
DIAAI_core = [""]
)"sv;
toml::table _data_table = toml::parse(source);
if (toml::array* arr = _data_table["config_paths"]["DIAAI_core"].as_array()) {
arr->for_each([](auto&& el) {
if constexpr (toml::is_string<decltype(el)>)
if(el == "")
el = "diaaiSetting.toml"sv;
});
}
std::cout << _data_table << std::endl;
return 0;
}
output:
[config_paths]
DIAAI_core = [ 'diaaiSetting.toml' ]值本身不是一个字符串,而是一个只包含一个值的字符串数组,因此反序列化它应该相当容易。
您可以轻松地在toml11中与值交互,因此如果这不适合您,您可能想看看它。
https://stackoverflow.com/questions/72674013
复制相似问题