Say that I have the string:
char* foo = " blah blee bleck ";
Now say that I want to read and throw away the first two words:
int bar = 0;
sscanf(foo, "%*s%n", &bar);
foo += bar;
if(bar > 0) sscanf(foo, "%*s%n, &bar);
My question is how can I tell if the second sscanf
read anything?
Do I need to 0 out bar
in between each read to determine if a string was actually read, or is there a better way?
EDIT:
Checking the sscanf
return value will not work because %*s and %n do not increase sscanf
's return value:
printf("%d ", sscanf(foo, "%*s%n", &bar));
printf("%d ", bar);
printf("%d ", sscanf(foo + bar, "%*s%n", &bar));
printf("%d\n", bar);
Will output:
0 6 0 6
Check the return value of sscanf call, as the document ion states an integer value would be returned.
I tried out a small piece of code in my machine similar to your query, and indeed sscanf returns negative values for unsuccessful operation.
Test code:
Test output for two scenarios:
The issue is the bar value is not set to zero for failed sscanf calls.