firstaccount firstaccount - 1 year ago 82
C++ Question

sscanf_s access violation when seperate string and integer

char a[200] = { 0 };

char tst[20] = "aaaa 123\n";
int i;
sscanf_s(tst, "%s %d",a, &i);
printf("reasult:%s %d", a,i);

No matter I use
char tst[20] = "aaaa 123\n";
char* tst = "aaaa 123\n";
it always shows access violation.
I need to seperate a string an integer from a string. But why this happens ?

Answer Source

sscanf_s expects two arguments for %c, %s and %[, the second being the size of the buffer passed. The following should work:

sscanf_s(tst, "%s %d", a, sizeof(a), &i);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download