fork download
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <string.h>
  4.  
  5. #define BUFSZ 4
  6. int main() {
  7. char attrValue[11]="keshakesha";
  8. char fmtString[10] = {0}; //there's no need for this one to be BUFSZ in length
  9. char scanBuf[BUFSZ];
  10.  
  11. //create a format string in fmtString; limit the scanned string to one less than BUFSZ
  12. snprintf(fmtString,sizeof(fmtString),"%%%ds",BUFSZ-1);
  13. printf("format string created for sscanf: %s\n", fmtString);
  14.  
  15. //scan from attrValue into scanBuf, using the format string created above
  16. int num=sscanf(attrValue,fmtString,scanBuf);
  17.  
  18. printf("items read: %d\n", num );
  19. printf("string read: %s\n", scanBuf );
  20.  
  21. return 0;
  22. }
  23.  
Success #stdin #stdout 0s 1832KB
stdin
Standard input is empty
stdout
format string created for sscanf: %3s
items read: 1
string read: kes