I have the following code snippet which is working fine:
ifstream NDSConfig( "NDS.config" ) ;
string szConfigVal ;
while( getline( NDSConfig, szConfigVal ) )
{
//code
}
But the problem is I need to update a check box state by comparing the line values. The code then will similar to the 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 I am going to use while loop then my state will be over riden and only last in the loop value or state will be updated. Is there any other way out. I need to set the check box values after reading from config file . The Config file looks as below:
AutoStart = 0
AutLogHistory = 1
AutoScan = 1
AutoMount = 0
AutoOpen = 0
LastConnectedSvr = 1
Though I can have only single if and everything else as else if will help but I need a better way.
Alternatively, use
boost::program_options, this is designed perfectly for what you require!EDIT: little more detail, program_options has a method to parse a config file, such as yours, and as you configure program_options, you can pass in the variable to store the value from the config file in. Have a look at their simple example, it will become abundantly clear…
Other option is for you to store your keys in a map, with a default value of 0, and then as you parse through your file, set the key to the value you read from the file…
EDIT:
using program options (this is untested code, please try refer to the documentation and fix as needed!)
When that lot runs, the various integers will have the values from the file (or be defaulted to 0).
The neat thing with this approach is that you can have different types in your config files, and as long as they are formatted as INI files, then this will work.
The other approach using std::map, well @Moo-Juice has already added working code…