User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,637 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,275 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Dec 30th, 2005
Views: 3,135
Last edited : Nov 25th, 2006.
c Syntax | 4 stars
  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. */
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 2:12 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC