Hi all,
I've written a configuration file parser, which is just basically gets a bunch of rules, each ended by ';'
To get the rules I use a loop:
ifstream cfgFile;
for (int iI = 0; iI < iMAX_MONITORS; iI ++) {
...
cfgFile >> someFloat;
// test for error
cfgFile >> someInt;
// test for error
...
}
and so on, pretty standard stuff. The problem is, every time I detect an error, all I can do is print the rule number, and not the line number, which means I have to then count the rules by hand to find the erroneous line, because >> ignores whitespace. Is there any way around this short of rewriting the function to use getline?
Is there some stream builtin that will give me a character number, or line number where I am up to in the stream?
All I can think of currently is to use a macro to get the input, and check for '\n':
(I've removed the \ to make it look nicer, and I haven't tested it, but you get the idea:)
#define EXPECT_INPUT(input, msg)
{
cfgFile >> input;
if (!cfgFile) {
if (cfgFile.eof ()) {
break;
}
printf ("error \"%s\" in config file, rule %d, line %d.", msg, iI + 1, iLines + 1);
cfgFile.clear();
cfgFile.ignore(std::numeric_limits<streamsize>::max(),';');
break;
}
do {
c=cfgFile.peek();
if (c == '\n') iLines ++;
if (c==' ' || c=='\n') c=cfgFile.get();
} while (c==' ' || c=='\n');
}
and then just do:
EXPECT_INPUT (someFloat, "a number");
EXPECT_INPUT (charArray, "a string");
which makes my function look nice, except that I have to use an ugly macro. Mayby I should just use getline - thoughts?
thanks heaps.