Is there a good way to loop over a string with sscanf
?
Let's say I have a string that looks like this:
char line[] = "100 185 400 11 1000";
and I'd like to print the sum. What I'd really like to write is this:
int n, sum = 0;
while (1 == sscanf(line, " %d", &n)) {
sum += n;
line += <number of bytes consumed by sscanf>
}
but there's no clean way to get that information out of sscanf
. If it returned the number of bytes consumed, that'd be useful. In cases like this, one can just use strtok
, but it'd be nice to be able to write something similar to what you can do from stdin
:
int n, sum = 0;
while (1 == scanf(" %d", &n)) {
sum += n;
// stdin is transparently advanced by scanf call
}
Is there a simple solution I'm forgetting?