Pages: [1]   Go Down
Author Topic: Splitting line into parts  (Read 737 times)
0 Members and 1 Guest are viewing this topic.
Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I read in a single line from a text file that contains a known delimiter, how can I:
a) split it up into the various parts
b) make sure that the respective parts are the respective types: a filename string, ints, and possibly others

It's also possible that one of the 'parts' is missing in a string, for example something like this:
Code:
filename.ext|5000|750   <-- all values present
filename.ext|2500|      <-- missing last value
filename.ext||500       <-- missing second value
filename.ext||          <-- missing all values
should all be considered valid.  The delimiters are *always* there, however anything after the filename is optional and should translate to either zero or null.  The program will test for those values and react accordingly.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


have a look at the strtok() , e.g. this example should help you - http://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm -
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome, that will get me going.  How do I go about setting each token to the correct type?  I'll have a string for filename, INTs, a float, possibly several of them per line ...
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

when they arrive over serial they are all characters.

the array of chars that represent a float can be converted with atof()  ints with atoi()   R

Read THE book on C from Kerningham and Ritchie, worth every penny and more.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: