c++ - How can I read a file line content using line number -
i have following code snippet working fine:
ifstream ndsconfig( "nds.config" ) ; string szconfigval ; while( getline( ndsconfig, szconfigval ) ) { //code }
but problem need update check box state comparing line values. code similar following:
ifstream ndsconfig( "nds.config" ) ; string szconfigval ; while( getline( ndsconfig, szconfigval ) ) { if(szconfigval == "autostart = 1") { //set check box true } else if(szconfigval == "autostart = 0") { //set check box false } if(szconfigval == "autloghistory = 1") { //set check box true } else if(szconfigval == "autloghistory = 0") { //set check box false } if(szconfigval == "autoscan = 1") { //set check box true } else if(szconfigval == "autoscan = 0") { //set check box false } if(szconfigval == "automount = 1") { //set check box true } else if(szconfigval == "automount = 0") { //set check box false } if(szconfigval == "autoopen = 1") { //set check box true } else if(szconfigval == "autoopen = 0") { //set check box false } if(szconfigval == "lastconnectedsvr = 1") { //set check box true } else if(szconfigval == "lastconnectedsvr = 0") { //set check box false } }
if going use while loop state on riden , last in loop value or state updated. there other way out. need set check box values after reading config file . config file looks below:
autostart = 0
autloghistory = 1
autoscan = 1
automount = 0
autoopen = 0
lastconnectedsvr = 1
though can have single if , else else if need better way.
alternatively, use boost::program_options
, designed require!
edit: little more detail, program_options has method parse config file, such yours, , configure program_options, can pass in variable store value config file in. have @ simple example, become abundantly clear...
other option store keys in map, default value of 0, , parse through file, set key value read file...
edit:
using program options (this untested code, please try refer documentation , fix needed!)
int autostart; int autloghistory; int autoscan; int automount; int autoopen; int lastconnectedsvr; po::options_description desc("allowed options"); desc.add_options() ("help", "produce message") ("autostart", po::value<int>(&autostart)->default_value(0),"autostart") ("autloghistory", po::value<int>(&autloghistory)->default_value(0),"autloghistory") ("autoscan", po::value<int>(&autoscan)->default_value(0),"autoscan") ("automount", po::value<int>(&automount)->default_value(0),"automount") ("autoopen", po::value<int>(&autoopen)->default_value(0),"autoopen") ("lastconnectedsvr", po::value<int>(&lastconnectedsvr)->default_value(0),"lastconnectedsvr") ; std::ifstream config("nds.config"); po::parse_command_line(config, desc, true);
when lot runs, various integers have values file (or defaulted 0).
the neat thing approach can have different types in config files, , long formatted ini files, work.
the other approach using std::map, @moo-juice has added working code...
Comments
Post a Comment