Originally Posted by
Nazgulled
doesn't work exactly what I want... first, you forgot the double quotes, it also works with 1,5,some text,h and I don't want it, it also works with 1,5,"some text", hv, and I also don't want that...
I'm sorry, I wasn't quite clear on your restrictions. This is the strictest format string you can give sscanf in order for it to work properly.
Code:
int num, num2;
char matchme[]="31,3,\"Fun in the sun\",v", str[40], ch[3];
if (sscanf(matchme, "%d,%d,\"%[^\"]\",%1[hv]", &num, &num2, str, ch ) < 4 )
printf("Error reading string.\n");
else {
char foo = ch[0];
printf("%d %d %s %c\n", num, num2, str, foo);
}
I am sorry, but the bracket identifier is more strict than %c or %s in both cases, since sscanf first reads two digits separated by a comma, a double quote and does nothing with it, then reads everything up to but excluding the next quote, then reads the quote and does nothing with it, then a comma, then a h or v. It fails if it doesn't match exactly. Pretty sweet!
The bracket identifier also requires a char array that must be large enough to hold all the input it should read and a terminating NUL. Why is it a problem for you to just take ch[] and assign the first argument to a normal char? It's not that hard or evil.