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
main.cpp@0:dacdce17672d, 2015-09-21 (annotated)
- Committer:
- rlanghbv
- Date:
- Mon Sep 21 18:04:40 2015 +0000
- Revision:
- 0:dacdce17672d
- Child:
- 1:ebe0744c248c
First commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rlanghbv | 0:dacdce17672d | 1 | #include "mbed.h" |
rlanghbv | 0:dacdce17672d | 2 | |
rlanghbv | 0:dacdce17672d | 3 | int main() |
rlanghbv | 0:dacdce17672d | 4 | { |
rlanghbv | 0:dacdce17672d | 5 | char buffer[] = "$LED,1,1,0,0,1,1,0,1\r\n"; //CSV string to be splitted |
rlanghbv | 0:dacdce17672d | 6 | |
rlanghbv | 0:dacdce17672d | 7 | //vars used to split the CSV string |
rlanghbv | 0:dacdce17672d | 8 | int count = 0; // counter |
rlanghbv | 0:dacdce17672d | 9 | char* tok; // holds the string element |
rlanghbv | 0:dacdce17672d | 10 | |
rlanghbv | 0:dacdce17672d | 11 | printf("\r\nAbout to splitt the CSV string: %s\r\n",buffer); |
rlanghbv | 0:dacdce17672d | 12 | |
rlanghbv | 0:dacdce17672d | 13 | tok = strtok(buffer, ","); // get the first element in string before the "," |
rlanghbv | 0:dacdce17672d | 14 | while(tok != NULL) |
rlanghbv | 0:dacdce17672d | 15 | { |
rlanghbv | 0:dacdce17672d | 16 | printf("%d %s\r\n",count,tok); // Display token |
rlanghbv | 0:dacdce17672d | 17 | count++; // inc counter |
rlanghbv | 0:dacdce17672d | 18 | tok = strtok(NULL, ","); // get the next token (next string item) |
rlanghbv | 0:dacdce17672d | 19 | } |
rlanghbv | 0:dacdce17672d | 20 | |
rlanghbv | 0:dacdce17672d | 21 | while (true) |
rlanghbv | 0:dacdce17672d | 22 | { |
rlanghbv | 0:dacdce17672d | 23 | wait(1); // wait one sec |
rlanghbv | 0:dacdce17672d | 24 | } |
rlanghbv | 0:dacdce17672d | 25 | } |