Go Down

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

Loren

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.

Thanks,

Loren

Osgeld

http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Arrch


SilkyPantsDan

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 :)

http://www.gammon.com.au/forum/?id=11119

PaulS

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

Go Up