0

sorry in advance for the formatting. Couldn't figure it out...

I'm passing a config file to a program via arguments

I'm trying to read a value from a specific parameter

I've got a cofigReader class with the following method for returning a string from a config file given a specific parameter

My problem,

it never finds the parameter. found is either 0 or -1....

string configReader::value(string config_file, string parameter)
{
    string value;
    char config_delimiter = '=';
    size_t found;
    file.open(config_file);
    std::string line;
    bool param_found = false;
    while(param_found == false){
        while (!file.eof())
        {       
            getline(file,line);
            logger.writetolog("INFO","Looking for " + parameter +
                         " on line "+ line); 
            found = line.find(parameter);
            logger.writetolog("INFO",int(found));
            if(found!=string::npos){
                param_found = true;
            }
        }
        param_found = true;
    }
    if (found!=string::npos)
    {   
        size_t a = line.find(config_delimiter)+1;
        logger.writetolog("INFO","Found" + parameter + 
                   "splitting string at delimter" + config_delimiter + 
                   " and return right side value");     
        value = line.substr(a);
        return value;
    }
    else
    {
        return value;
    }
    file.close();
}

more info. Config file reads like this.

toemail=someemailaddress@gmail.com
outputdir=C:\tmp

configReader class used like this

//attempt to parse out the required parameters for the program
string toemail = config.value(configFileArg,"toemail"); 

it ALWAYS return empty

Tom Kerr
  • 9,632
  • 1
  • 26
  • 43
nkuebelbeck
  • 273
  • 2
  • 5
  • 15
  • 1
    If `found` is `0` then the parameter _was_ found at the start of the string. What does the output look like? – Chad Dec 19 '12 at 17:37

2 Answers2

3

Your while (!file.eof()) loop continues after you find a match, over-writing the value of found which you check later.

You could fix this by changing your loop to something like

bool param_found = false;
while (!param_found && !file.eof()) {       
    if (getline(file,line)) {
        break;
    }
    logger.writetolog("INFO","Looking for " + parameter +" on line "+ line); 
    found = line.find(parameter);
    logger.writetolog("INFO",int(found));
    if(found!=string::npos){
        param_found = true;
        break;
    }
}

instead. (Notice that this code removes your while(param_found == false) loop. As sftrabbit points out, that loop is unnecessary.)

Nawaz
  • 327,095
  • 105
  • 629
  • 812
simonc
  • 39,997
  • 11
  • 78
  • 100
  • 1
    Also, the outer `while` loop is pointless. It only ever loops once. – Joseph Mansfield Dec 19 '12 at 17:40
  • Yep, thanks, +1. I'd noticed that but you just beat me to it with your post :) – simonc Dec 19 '12 at 17:42
  • 1
    What if `getline(file,line);` fails? The loop should be written as `while (getline(file,line))` and you can break it anytime, setting whatever variables to whatever you want. – Nawaz Dec 19 '12 at 17:43
  • Thanks, your code works. Learning is difficult, but easier with stack. – nkuebelbeck Dec 19 '12 at 17:44
  • @Nawaz Do you mean the lack of test for `getline` failure? I'd omitted that rather than copy your answer. In hindsight, an accepted answer that isn't quite right isn't ideal so I've updated my answer with an alternative attempt at error handling. If the loop is still wrong can you be more specific about the other problems please? – simonc Dec 19 '12 at 18:39
2

The idiomatic way to write loop is this:

bool param_found = false;

while (std::getline(file,line)) //<-- this is idiomatic loop!
{                               //successfully read OR exit from the loop

    logger.writetolog("INFO","Looking for " + parameter +" on line "+ line); 
    found = line.find(parameter);
    logger.writetolog("INFO",int(found));
    if(found!=string::npos){
        param_found = true;
        break;
    }
}

You should not use eof() when writing loops:

These two topics discuss this in detail.

Community
  • 1
  • 1
Nawaz
  • 327,095
  • 105
  • 629
  • 812