Amatorcpp Temat założony przez niniejszego użytkownika |
BOOST, konfiguracja » 2020-07-07 22:51:36 Hej chciałem zrobić system konfiguracji w c++ na bibliotece boost ale gdy czegoś brakuje pliku np .ini, jakiegoś zapisu to program się wywala kod #include "Config.h" #include"boost/property_tree/ptree.hpp" #include<boost/property_tree/json_parser.hpp> #include"var.h" Config * config; #define gvn(a) (#a) void Config::SaveLauncher() { boost::property_tree::ptree py; py.put( gvn( set.FontSize1 ), set.FontSize1 ); py.put( gvn( set.FontSize2 ), set.FontSize2 ); py.put( gvn( set.FontSize3 ), set.FontSize3 ); py.put( gvn( set.FontSize4 ), set.FontSize4 ); boost::property_tree::write_json( "/Launcher.txt", py ); } void Config::LoadLauncher() { boost::property_tree::ptree py; set.FontSize1 = py.get < int >( gvn( set.FontSize1 ) ); set.FontSize2 = py.get < int >( gvn( set.FontSize2 ) ); set.FontSize3 = py.get < int >( gvn( set.FontSize3 ) ); set.FontSize4 = py.get < int >( gvn( set.FontSize4 ) ); boost::property_tree::read_json( "/Launcher.txt", py ); }
I gdy w pliku "Launcher.txt" nie ma jednej zmiennej to program się sypie |
|
pekfos |
» 2020-07-07 22:58:41 https://www.boost.org/doc/libs/1_65_1/doc/html/property_tree/accessing.html#property_tree.accessing.three_ways_of_getting_dataTu masz opisane sposoby na wyciąganie danych. Używasz wersji wyrzucającej wyjątek, jeśli danych nie ma. Nieobsłużony wyjątek spowoduje krytyczny błąd programu. This call locates the proper node in the tree and tries to translate its data string to a float value. If that fails, exception is thrown. If path does not exist, it will be ptree_bad_path exception. If value could not be translated, it will be ptree_bad_data. Both of them derive from ptree_error to make common handling possible. |
Do pliku konfiguracyjnego proponowałbym wersje z wartością domyślną. boost::property_tree::ptree py; set.FontSize1 = py.get < int >( gvn( set.FontSize1 ) ); set.FontSize2 = py.get < int >( gvn( set.FontSize2 ) ); set.FontSize3 = py.get < int >( gvn( set.FontSize3 ) ); set.FontSize4 = py.get < int >( gvn( set.FontSize4 ) ); boost::property_tree::read_json( "/Launcher.txt", py ); |
Poza tym tutaj próbujesz pobrać wartości z pliku, którego jeszcze nie wczytałeś. |
|
Amatorcpp Temat założony przez niniejszego użytkownika |
» 2020-07-07 23:04:53 "Poza tym tutaj próbujesz pobrać wartości z pliku, którego jeszcze nie wczytałeś."
Gdy są wszystkie dane w pliku normalnie mi się ładują dane ;) |
|
pekfos |
» 2020-07-07 23:13:41 "Poza tym tutaj próbujesz pobrać wartości z pliku, którego jeszcze nie wczytałeś."
Gdy są wszystkie dane w pliku normalnie mi się ładują dane ;) |
Dla podanego kodu - niemożliwe. |
|
Amatorcpp Temat założony przez niniejszego użytkownika |
» 2020-07-08 15:41:18 Stary, mam taką prośbę, nie odpowiadaj na moje pytania na tym forum Dzięki :P |
|
pekfos |
» 2020-07-08 18:29:39 Jakie ma znaczenie, kto Ci powie że jesteś w błędzie? Naprawdę potrzebujesz demonstracji, że Twój kod nie ma prawa działać? Ok. #include <iostream> #include"boost/property_tree/ptree.hpp" #include<boost/property_tree/json_parser.hpp>
#define gvn(a) (#a)
int main() { try { boost::property_tree::ptree py; int FontSize1 = py.get < int >( gvn( set.FontSize1 ) ); std::cout << "Loaded font size " << FontSize1 << '\n'; int FontSize2 = py.get < int >( gvn( set.FontSize2 ) ); int FontSize3 = py.get < int >( gvn( set.FontSize3 ) ); int FontSize4 = py.get < int >( gvn( set.FontSize4 ) ); std::cout << "Loading file\n"; boost::property_tree::read_json( "/Launcher.txt", py ); } catch(...) { std::cout << ":(\n"; } } ubuntu@q43:~/boost$ g++ ~/shared/boost.cpp -o a -I boost_1_73_0 -g ubuntu@q43:~/boost$ gdb ./a GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./a...done. (gdb) catch throw Catchpoint 1 (throw) (gdb) r Starting program: /home/ubuntu/boost/a
Catchpoint 1 (exception thrown), 0x00007ffff7adeced in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (gdb) bt #0 0x00007ffff7adeced in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #1 0x000055555555a95f in boost::throw_exception<boost::property_tree::ptree_bad_path> (e=..., loc=...) at boost_1_73_0/boost/throw_exception.hpp:171 #2 0x0000555555559ca7 in boost::property_tree::basic_ptree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::get_child ( this=0x7fffffffde50, path=...) at boost_1_73_0/boost/property_tree/detail/ptree_implementation.hpp:576 #3 0x000055555555952d in boost::property_tree::basic_ptree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::get_child ( this=0x7fffffffde50, path=...) at boost_1_73_0/boost/property_tree/detail/ptree_implementation.hpp:585 #4 0x0000555555559069 in boost::property_tree::basic_ptree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::get<int> ( this=0x7fffffffde50, path=...) at boost_1_73_0/boost/property_tree/detail/ptree_implementation.hpp:751 #5 0x0000555555557b42 in main () at /home/ubuntu/shared/boost.cpp:12 (gdb) frame 5 #5 0x0000555555557b42 in main () at /home/ubuntu/shared/boost.cpp:12 12 int FontSize1 = py.get < int >( gvn( set.FontSize1 ) ); (gdb) c Continuing. :( [Inferior 1 (process 29357) exited normally] Wszystko zgodnie z dokumentacją. Nie rozumiem po co udajesz, że ten kod działa. |
|
DejaVu |
» 2020-07-08 19:06:48 @Amatorcpp: ja też mam prośbę - szanuj osoby, które mają znacznie większe umiejętności programistyczne niż Ty obecnie, które próbują Ci pomóc. Jak pekfos Ci nie pomoże to nikt inny Ci nie pomoże. |
|
Amatorcpp Temat założony przez niniejszego użytkownika |
» 2020-07-08 20:00:35 Ta bo prefoks to jakiś król c++? Te admin, kolejną prośbę, usuń moje konto z tego serwisu
dzięki |
|
« 1 » 2 |