Go Down

Topic: Splitting line into parts (Read 817 times) previous topic - next topic

KirAsh4

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: [Select]
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.

robtillaart


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

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

KirAsh4

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 ...

robtillaart

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.
Rob Tillaart

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

Go Up