The %n specifier will give the number of characters processed by the scan and store it in an int.
Accumulate those in an offset to iterate through the line.
This sscanf could return EOF, 0, 1 or 2. Consider using == 2 since you want to scan two integers.
char buffer[100];
int offset = 0;
int scanned = 0;
while ( fgets ( buffer, sizeof buffer, stdin) != NULL) {
offset = 0;
scanned = 0;
while ( sscanf ( buffer + offset, "%*[^0-9|-]%i%*[^0-9|-]%i%n", &key, &value, &scanned) == 2) {
offset += scanned;
printf("key==%d,value==%d\n",key,value);
}
}
In event %n does not work, strchr could be used to find a (, sscanf the ( and two integers, then find the closing ). Repeat.
This uses @WeatherVane's suggestion of a MUCH simpler format string.
char buffer[100];
char *begin = NULL;
char *end = NULL;
while ( fgets ( buffer, sizeof buffer, stdin) != NULL) {
end = buffer;
while ( begin = strchr ( end, '(')) {//get pointer to (
if ( sscanf ( begin, "(%d ,%d", &key, &value) == 2) {//scan ( and two integers
printf("key==%d,value==%d\n",key,value);
}
end = strchr ( begin, ')');//get pointer to )
if ( ! end) {
break;//did not find )
}
}
}
Another strategy could use strspn and strcspn
char buffer[100];
size_t begin = 0;
size_t count = 0;
while ( fgets ( buffer, sizeof buffer, stdin) != NULL) {
begin = 0;
while ( buffer[begin]) {// not zero
count = strcspn ( &buffer[begin], "-0123456789");//count of non digit or -
begin += count;
if ( sscanf ( &buffer[begin], "%d ,%d", &key, &value) == 2) {//scan ( and two integers
printf("key==%d,value==%d\n",key,value);
}
count = strspn ( &buffer[begin], " ,-0123456789");//count space , - or digit
begin += count;
}
}
" (%d ,%d )%n"should also work, the space included before each punctuation will filter any whitespace. - Weather Vane