944,205 Members | Top Members by Rank

Ad:
  • C Code Snippet
  • Views: 8364
  • C RSS
0

Parsing a String into Tokens Using strcspn, Part 3

by on Dec 30th, 2005
Yet another angle at this same topic.

See also Parsing a String into Tokens Using strcspn, Part 1 and Parsing a String into Tokens Using strcspn, Part 2.
C Code Snippet (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(void)
  5. {
  6. static const char filename[] = "file.txt"; /* the name of a file to open */
  7. FILE *file = fopen(filename, "r"); /* try to open the file */
  8. if ( file != NULL )
  9. {
  10. char line[BUFSIZ]; /* space to read a line into */
  11. /*
  12.   * Create an array of strings: here each line may be broken up into up
  13.   * to 20 strings of up to 32 characters. Try changing it to 10 to hit
  14.   * the "no more room" message.
  15.   */
  16. char data[20][32];
  17. while ( fgets(line, sizeof line, file) != NULL ) /* read each line */
  18. {
  19. size_t i = 0, size;
  20. char *token = line;
  21. fputs(line, stdout);
  22. for ( ;; )
  23. {
  24. size_t len = strcspn(token, ";\n"); /* search for delimiters */
  25. /*
  26.   * Use sprint to copy the text into a char array.
  27.   */
  28. sprintf(data[i], "%.*s", (int)len, token);
  29.  
  30. token += len; /* advance pointer by the length of the found text */
  31. if ( *token == '\0' )
  32. {
  33. break; /* advanced to the terminating null character */
  34. }
  35. ++token; /* skip the delimiter */
  36.  
  37. /* advance the index into the string array */
  38. if ( ++i >= sizeof data / sizeof *data )
  39. {
  40. puts("no more room");
  41. break;
  42. }
  43. }
  44. /*
  45.   * Display the results.
  46.   */
  47. for ( size = i, i = 0; i < size; ++i )
  48. {
  49. printf("data[%d] = \"%s\"\n", (int)i, data[i]);
  50. }
  51. puts("---");
  52. }
  53. fclose(file);
  54. }
  55. else
  56. {
  57. perror(filename); /* why didn't the file open? */
  58. }
  59. return 0;
  60. }
  61.  
  62. /* file.txt
  63. 2;31May2005;23:59:00;100.2.1.12;log;accept;;eth9;inbound;VPN-1
  64. FireWall-1;;100.1.20.130;172.30.7.114;icmp;191;;;8;0;;;;
  65. */
  66.  
  67. /* my output
  68. 2;31May2005;23:59:00;100.2.1.12;log;accept;;eth9;inbound;VPN-1
  69. data[0] = "2"
  70. data[1] = "31May2005"
  71. data[2] = "23:59:00"
  72. data[3] = "100.2.1.12"
  73. data[4] = "log"
  74. data[5] = "accept"
  75. data[6] = ""
  76. data[7] = "eth9"
  77. data[8] = "inbound"
  78. data[9] = "VPN-1"
  79. ---
  80. FireWall-1;;100.1.20.130;172.30.7.114;icmp;191;;;8;0;;;;
  81. data[0] = "FireWall-1"
  82. data[1] = ""
  83. data[2] = "100.1.20.130"
  84. data[3] = "172.30.7.114"
  85. data[4] = "icmp"
  86. data[5] = "191"
  87. data[6] = ""
  88. data[7] = ""
  89. data[8] = "8"
  90. data[9] = "0"
  91. data[10] = ""
  92. data[11] = ""
  93. data[12] = ""
  94. data[13] = ""
  95. */
Message:
Previous Thread in C Forum Timeline: Parsing a String into Tokens Using strcspn, Part 2
Next Thread in C Forum Timeline: remove method linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC