#include <stdio.h>
#include <assert.h>
#include <string.h>
#define BUFSZ 4
int main() {
char attrValue[11]="keshakesha";
char fmtString[10] = {0}; //there's no need for this one to be BUFSZ in length
char scanBuf[BUFSZ];
//create a format string in fmtString; limit the scanned string to one less than BUFSZ
snprintf(fmtString
,sizeof(fmtString
),"%%%ds",BUFSZ
-1); printf("format string created for sscanf: %s\n", fmtString
);
//scan from attrValue into scanBuf, using the format string created above
int num
=sscanf(attrValue
,fmtString
,scanBuf
);
printf("items read: %d\n", num
); printf("string read: %s\n", scanBuf
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxhc3NlcnQuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBCVUZTWiA0IAppbnQgbWFpbigpIHsKICAgIGNoYXIgYXR0clZhbHVlWzExXT0ia2VzaGFrZXNoYSI7CiAgICBjaGFyIGZtdFN0cmluZ1sxMF0gPSB7MH07IC8vdGhlcmUncyBubyBuZWVkIGZvciB0aGlzIG9uZSB0byBiZSBCVUZTWiBpbiBsZW5ndGgKICAgIGNoYXIgc2NhbkJ1ZltCVUZTWl07CgogICAgLy9jcmVhdGUgYSBmb3JtYXQgc3RyaW5nIGluIGZtdFN0cmluZzsgbGltaXQgdGhlIHNjYW5uZWQgc3RyaW5nIHRvIG9uZSBsZXNzIHRoYW4gQlVGU1oKICAgIHNucHJpbnRmKGZtdFN0cmluZyxzaXplb2YoZm10U3RyaW5nKSwiJSUlZHMiLEJVRlNaLTEpOwogICAgcHJpbnRmKCJmb3JtYXQgc3RyaW5nIGNyZWF0ZWQgZm9yIHNzY2FuZjogJXNcbiIsIGZtdFN0cmluZyk7CiAgICAKICAgIC8vc2NhbiBmcm9tIGF0dHJWYWx1ZSBpbnRvIHNjYW5CdWYsIHVzaW5nIHRoZSBmb3JtYXQgc3RyaW5nIGNyZWF0ZWQgYWJvdmUKICAgIGludCBudW09c3NjYW5mKGF0dHJWYWx1ZSxmbXRTdHJpbmcsc2NhbkJ1Zik7CiAgICAKICAgIHByaW50ZigiaXRlbXMgcmVhZDogJWRcbiIsIG51bSApOwogICAgcHJpbnRmKCJzdHJpbmcgcmVhZDogJXNcbiIsIHNjYW5CdWYgKTsKCiAgICByZXR1cm4gMDsKfQo=