Example show how to split up a CSV string using strtok

Dependencies:   mbed

Import program

00001 #include "mbed.h"
00002 
00003 int main() 
00004 {   
00005     char buffer[] = "$LCD,Line 1,Line 2\r\n";  //CSV string  to be splitted
00006     
00007     //vars used to split the CSV string
00008     int count = 0;  // counter
00009     char* tok;      // holds the string element
00010     
00011     printf("\r\nAbout to splitt the CSV string: %s\r\n",buffer);
00012     
00013     tok = strtok(buffer, ",");   // get the first element in string before the ","
00014     while(tok != NULL) 
00015     {
00016         printf("%d %s\r\n",count,tok);  // Display token
00017         count++;                        // inc counter
00018         tok = strtok(NULL, ",");        // get the next token (next string item) 
00019     } 
00020         
00021     while (true) 
00022     {
00023         wait(1); // wait one sec
00024     }
00025 }
00026 

Files at this revision

API Documentation at this revision

Comitter:
rlanghbv
Date:
Mon Oct 05 08:25:43 2015 +0000
Parent:
1:ebe0744c248c
Commit message:
Removed empty ,

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r ebe0744c248c -r 6feb81b10d16 main.cpp
--- a/main.cpp	Mon Oct 05 08:17:07 2015 +0000
+++ b/main.cpp	Mon Oct 05 08:25:43 2015 +0000
@@ -2,7 +2,7 @@
 
 int main() 
 {   
-    char buffer[] = "$LCD,Line 1,Line 2,\r\n";  //CSV string  to be splitted
+    char buffer[] = "$LCD,Line 1,Line 2\r\n";  //CSV string  to be splitted
     
     //vars used to split the CSV string
     int count = 0;  // counter