This is a function I wrote a very long time ago to do just that.
You would call it like:
// char char_var_to_store_result_in should be an array large enough for the value.
getstrfld (your_line_to_parse_from, 0, 0, ",", char_var_to_store_result_in);
// Do something with the data...
getstrfld (your_line_to_parse_from, 1, 0, ",", char_var_to_store_result_in);
// Do something with the data...
You will have to do an atoi on the char_var_to_store_result_in to get the real number value.
The field number is zero-based, so the first field would be 0 the next 1 and so on.
char *getstrfld (char *strbuf, int fldno, int ofset, char *sep, char *retstr) { char *offset, *strptr; int curfld; if (!*strbuf) { *retstr = 0; return (retstr); } offset = strptr = (char *) NULL; curfld = 0; strbuf += ofset; while (*strbuf) { strptr = !offset ? strbuf : offset; offset = strpbrk ((!offset ? strbuf : offset), sep); if (offset) offset++; else if (curfld != fldno) { *retstr = (char) 0; break; } if (curfld == fldno) { strncpy (retstr, strptr, (int)(!offset ? strlen (strptr) + 1 : (int) (offset - strptr))); if (offset) retstr[offset - strptr - 1] = 0; break; } curfld++; } return retstr; }
Hi Triform,
Thanks for the reply. However my programing level is low enough that the code you posted looks like a foreign language to me.
Right from the first line I have no idea What all these things mean?? (char *strbuf, int fldno, int ofset, char *sep, char *retstr) { char *offset, *strptr; int curfld; if (!*strbuf) { *retstr = 0; return (retstr); }
Is there a Dumbed down version.
Thanks
Cheers
L
This is the way your code came out in my email. I thought before I read you actual msg that you were making a joke. My response was going to be " Ok that is just cruel" 🙂
After all this is how c programmers would write their code in some competitions.
char *getstrfld (char *strbuf, int fldno, int ofset, char *sep, char *retstr); void setup() { char val_buffer[10]; int val_one = 0, val_two = 0; Serial.begin (9600); getstrfld ("123,456", 0, 0, ",", val_buffer); val_one = atoi (val_buffer); Serial.println (val_one); getstrfld ("123,456", 1, 0, ",", val_buffer); val_two = atoi (val_buffer); Serial.println (val_two); } void loop() { // put your main code here, to run repeatedly: } char *getstrfld (char *strbuf, int fldno, int ofset, char *sep, char *retstr) { char *offset, *strptr; int curfld; if (!*strbuf) { *retstr = 0; return (retstr); } offset = strptr = (char *) NULL; curfld = 0; strbuf += ofset; while (*strbuf) { strptr = !offset ? strbuf : offset; offset = strpbrk ((!offset ? strbuf : offset), sep); if (offset) offset++; else if (curfld != fldno) { *retstr = (char) 0; break; } if (curfld == fldno) { strncpy (retstr, strptr, (int)(!offset ? strlen (strptr) + 1 : (int) (offset - strptr))); if (offset) retstr[offset - strptr - 1] = 0; break; } curfld++; } return retstr; }
Pat Wicker (Portland, OR, USA)
Here is an example that uses C++, the language of choice, that the Arduino designers and implementer's wanted to, and intended for you to use:
void setup() { Serial.begin(9600); String line = " Left , Right "; int delimPos = line.indexOf(','); // Start from position 0, and read up to, not including "delimPos" characters String left = line.substring(0, delimPos); // Start from next character following "delimPos", and read remaining characters String right = line.substring(delimPos + 1); // Remove leading and trailing spaces left.trim(); right.trim(); Serial.print(left); Serial.print(right); }
Although Arduino supports code written in both "Assembly" and "C" as shown, I suggest you stick to the "C++" language unless you have a need to do otherwise!
By the way, you can easily incorporate the above example I provided into a loop too, if you need to read and work with multiple lines from a an SD file in real time.
Hope this help's!
Hi Frogandtoad,
Great handle by the way.
Thanks for the code. It gives me a good starting point. I'll play around with it and let you know how it goes.
Cheers
L
No problem!
Here is a good reference to bookmark, for all Arduino programmers who wish to understand most parts of the language:
Cheers!