- Joined
- Jan 13, 2008
- Messages
- 2,303
- Reaction score
- 6
- Points
- 38
- Location
- Atlanta, GA, USA, North America
I'm having some problems parsing a variable length string of numbers in C++. Basically, I have a string that has an int, a sequence of ints equal to the first int, then several doubles. For example:
This is my latest attempt at parsing this using vertical pipes around the variable length section, but it won't work because the ints might be more than one character long and in this solution, I'm only incrementing one character at a time:
So does anyone have any ideas how to parse this?
Thanks for any help you can provide,
Matt
The number of numbers in bold, is equal to the first number. So if I have a 1, then I'll only have one more number before the 0.0 0.0 1.0 1.0 0.0 0.0 45.0 sequence, if I have a 3, I'll have 3 numbers in there etc. I'm having some problems parsing this string however. If it was fixed width, I'd just use sscanf with the proper format, but the code won't know how many ints are in the bolded section, until it reads the first int.5 8 9 10 12 14 0.0 0.0 1.0 1.0 0.0 0.0 45.0
This is my latest attempt at parsing this using vertical pipes around the variable length section, but it won't work because the ints might be more than one character long and in this solution, I'm only incrementing one character at a time:
Code:
// char* strr = "5|8|9|10|12|14|0.0 0.0 1.0 1.0 0.0 0.0 45.0" <- effectively the state of things at this point
char* pch = strtok(strr,"|");
int i = 0;
while(pch != NULL)
{
sscanf(pch,"%d",ram->grouplist[i]);
pch = strtok(NULL,"|");
i++;
}
char strv[1024];
strcpy(strv,strr);
sscanf(strv,"%lf %lf %lf %lf %lf %lf %lf",ram->pos.x,ram->pos.y,ram->pos.z,ram->axis.x,ram->axis.y,ram->axis.z,ram->angle);
Thanks for any help you can provide,
Matt
Last edited: