|
Boost Users : |
Subject: Re: [Boost-users] How to use the optional with property_treeâ
From: çéæ§ (wyhfly_at_[hidden])
Date: 2013-12-19 09:28:13
Those following lines from your code should give you a hint if (child2) { child2.get().put_value(strValue); } else { child2.get().put_value(strValue); }
Could anyone help? Why the below code will crash on the below line? I'm using the boost property_tree. Thanks. child1.get().put(sub_field, strValue); ========================================================= #include <fstream> #include <boost/filesystem.hpp> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ini_parser.hpp> #include <boost/any.hpp> #include <boost/optional.hpp> void writeConfig(const std::string &field, const std::string &sub_field, const std::string & strValue) { if (!boost::filesystem::exists(prog_cfg_file)) { std::ofstream ofIniConfig(prog_cfg_file, std::ofstream::out|std::ofstream::app); ofIniConfig.close(); } boost::property_tree::ptree pt; boost::property_tree::ini_parser::read_ini(prog_cfg_file, pt); boost::optional<boost::property_tree::ptree&> child1 = pt.get_child_optional(field); if (child1) { boost::optional<boost::property_tree::ptree&> child2 = child1.get().get_child_optional(sub_field); if (child2) { child2.get().put_value(strValue); }else { child2.get().put_value(strValue); } }else { child1.get().put(sub_field, strValue); } boost::property_tree::ini_parser::write_ini(prog_cfg_file, pt); } int main() { writeConfig("test", "a", "OK"); return 0; }
--
兰生幽谷, 不以无人而不芳...
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I still don't understandï¼could you please figure it outï¼thanks.
åéèªé
æææº
-------- åå§é®ä»¶ --------
å件人ï¼Olivier Tristan <o.tristan_at_[hidden]>
æ¶é´ï¼å¨å 12æ19æ¥ 22:08
æ¶ä»¶äººï¼boost-users_at_[hidden]
主é¢ï¼Re: [Boost-users] How to use the optional with property_treeâ
>Those following lines from your code should give you a hint
>
>if (child2)
>{
>child2.get().put_value(strValue);
>}
>else
>{
>child2.get().put_value(strValue);
>}
>
>
>
>Le 19/12/2013 12:34, çéæ§ a écrit :
>> Could anyone help? Why the below code will crash on the below line? I'm using the boost property_tree. Thanks.
>>
>> child1.get().put(sub_field, strValue);
>>
>> =========================================================
>> #include <fstream>
>> #include <boost/filesystem.hpp>
>> #include <boost/property_tree/ptree.hpp>
>> #include <boost/property_tree/ini_parser.hpp>
>> #include <boost/any.hpp>
>> #include <boost/optional.hpp>
>>
>> void writeConfig(const std::string &field, const std::string &sub_field, const std::string & strValue)
>> {
>> if (!boost::filesystem::exists(prog_cfg_file))
>> {
>> std::ofstream ofIniConfig(prog_cfg_file, std::ofstream::out|std::ofstream::app);
>> ofIniConfig.close();
>> }
>>
>>
>> boost::property_tree::ptree pt;
>> boost::property_tree::ini_parser::read_ini(prog_cfg_file, pt);
>> boost::optional<boost::property_tree::ptree&> child1 = pt.get_child_optional(field);
>>
>> if (child1)
>> {
>> boost::optional<boost::property_tree::ptree&> child2 = child1.get().get_child_optional(sub_field);
>>
>> if (child2)
>> {
>> child2.get().put_value(strValue);
>> }else
>> {
>> child2.get().put_value(strValue);
>> }
>> }else
>> {
>> child1.get().put(sub_field, strValue);
>> }
>>
>> boost::property_tree::ini_parser::write_ini(prog_cfg_file, pt);
>> }
>>
>> int main()
>> {
>> writeConfig("test", "a", "OK");
>> return 0;
>> }
>>
>>
>>
>>
>> --
>> å
°ç幽谷, ä¸ä»¥æ 人èä¸è³...
>>
>>
>>
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>
>
>--
>Olivier TRISTAN
>uvi.net
>
>_______________________________________________
>Boost-users mailing list
>Boost-users_at_[hidden]
>http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net