An equivalent of sscanf
I usually read a config file for my application with a " : " delimiter e.g. :
And after I read the whole line into a string, I "tokenize" it with sscanf like so:
#name : filename : value : boolstate (0 = false / 1 = true)
myConf : config.txt : 2000 : 1
I understand sscanf is a method from C, so if I wanted to make it more C++, what should I use? Or maybe there's some better way to scan the values from the string that can directly convert the variable type (e.g. from "1000" from the file to int(1000) )? Thanks in advance.
int value, tempBool;
res = sscanf ( buffer, "%s : %s : %d : %d", tempName, tempFile, &value, &tempBool);
have you tried stringstream?
Nope. Never use that before. Okay, I'll try that one. Thanks. :D
Originally Posted by vart
Okay I've tried the stringstream. But why can't it convert '0' to 0 but to 48 ? E.g. I have this code:
Can anybody help me with this? Thanks.
unsigned int value;
mystream>>value; //Value becomes 48, not the intended 0
EDIT: My bad. It turns put to be an unsigned char, not unsigned int. Sorry, didn't check it thoroughly. :D
for an nicer way of string2int you can also try
'0' is a integer constant
so probably you want this:
std::string inputstr("0 15");
unsigned int value1, value2;
mystream>>value1 >> value2; //Value1 is 0, value 2 is 15