Pages: [1]   Go Down
Author Topic: Parsing csv file from sd card to an array  (Read 2142 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 0
Posts: 155
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what like in a spreadsheet?
Logged


California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What have you tried so far?
Logged

Radelaide!
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50500
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: