I would like to read key value pairs from a file, while ignoring comment lines.
imagine a file like:
key1=value1
#ignore me!
I came up with this,
a) it seems very clunky and
b) it doesn't work if the '=' isn't surrounded by spaces. The lineStream isn't being properly split and the entire line is being read into "key".
std::ifstream infile(configFile);
std::string line;
std::map<std::string, std::string> properties;
while (getline(infile, line)) {
//todo: trim start of line
if (line.length() > 0 && line[0] != '#') {
std::istringstream lineStream(line);
std::string key, value;
char delim;
if ((lineStream >> key >> delim >> value) && (delim == '=')) {
properties[key] = value;
}
}
}
Also, comments on my code style are welcome :)