Using "%n"
works fine. @ user3121023
Recommend with " %n"
to resolve the optional trailing space, for example << 22>, go through "V2X_3_expected\n"
and check the result of %n
on "V2X_3_expected 123"
.
<
char entry[] = "V2X_3_accepted"; int d1,d2; int n1 = 0; int n2 = 0; sscanf(entry,"V2X_%d_expected %n",&d1, &n1); sscanf(entry,"V2X_%d_received %n",&d2, &n2); if (n1 > 0 && entry[n1] == '\0') Success_expected(d1); else if (n2 > 0 && entry[n2] == '\0') Success_received(d2); else Fail(entry);
Initialize n1
to a value that will never be set; the scan reached the qualifier "%n"
. n1 = 0;
works well in most cases, for example, in the OP format "V2X_%d_ ..."
.
n1 = -1; /* and (n1 >= 0 */
n1 = -1; /* and (n1 >= 0 */
also works with short formats such as " %n"
.
source share