Go Down

Topic: Parsing csv file from sd card to an array (Read 4277 times) previous topic - next topic


I have searched hi and low and cannot find anyway to parse a csv file into array. 

I can open the file and write the whole file to the serial port, but I have not been able to figure out how to use delimiters to break it up into lines and columns.

If anyone could help me find a tutorial or get me pointed in the right direction I would certainly appreciate it.







There are a few way to do this, and it really depends on how 'funny' your data can be.

One way is to use something like strtok and strtok_rto split the C string when it comes across a certain character in a set, although this can have trouble with strings that contain the same delimiters.

Another way is to write your own parser - which isn't too hard. The basic idea is to read each character as it comes (whether from streaming, or loading the file into a C string array and manually walking it) and split out the values as you go along. This way also allows you to have strings that share the delimiter, or adding special tokens that you can parse later.

Of course that's just the idea of writing one, and getting it to function on a micro controller will take a bit more restraint in terms of memory etc. There are a few examples, but most look to use the STL libraries, which I'm not too sure how they will run on a micro controller.

Actually - with a bit of work you might be able to use them :)



One way is to use something like strtok and strtok_r to split the C string

strtok(), yes. strtok_r(), no!

The strtok_r() function is the thread-safe version of strtok(). On a single-threaded system, like the Arduino, there is no reason to use the much more complicated, larger foot-print, function when the non-thread-safe version is easier to use, has a smaller foot print, and its data can not be trashed by another thread.
The art of getting good answers lies in asking good questions.

Go Up