DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   c (http://www.daniweb.com/code/c.html)
-   -   Parsing a String into Tokens Using sscanf (http://www.daniweb.com/code/snippet281.html)

Dave Sinkula c syntax
Jun 4th, 2005
Many times strtok is recommended for parsing a string; I don't care for strtok. Why?
  • It modifies the incoming string, so it cannot be used with string literals or other constant strings.
  • The identity of the delimiting character is lost.
  • It uses a static buffer while parsing, so it's not reentrant.
  • It does not correctly handle "empty" fields -- that is, where two delimiters are back-to-back and meant to denote the lack of information in that field.
This snippet shows a way to use sscanf to parse a string into fields delimited by a character (a semicolon in this case, but commas or tabs or others could be used as well).

Thanks to figo2476 for pointing out an issue with a previous version!
Thanks to dwks for asking why not to use strtok.

  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. const char line[] = "2004/12/03 12:01:59;info1;info2;info3";
  6. const char *ptr = line;
  7. char field [ 32 ];
  8. int n;
  9. while ( sscanf(ptr, "%31[^;]%n", field, &n) == 1 )
  10. {
  11. printf("field = \"%s\"\n", field);
  12. ptr += n; /* advance the pointer by the number of characters read */
  13. if ( *ptr != ';' )
  14. {
  15. break; /* didn't find an expected delimiter, done? */
  16. }
  17. ++ptr; /* skip the delimiter */
  18. }
  19. return 0;
  20. }
  21.  
  22. /* my output
  23. field = "2004/12/03 12:01:59"
  24. field = "info1"
  25. field = "info2"
  26. field = "info3"
  27. */