Pages: [1]   Go Down
Author Topic: strtok help  (Read 724 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am receiving ascii over serial into an array, and then using atoi function to convert this into an integer. At the moment I am sending just one acsii representation that I want in an integer in a variable on the recieving end.

I would like to extend this to 4 values, with : in between each so that I can use strtok. The problem is I want to store each one into a different value, eg first value in x variable, second in m variable, 3rd in a variable etc etc

I cant seem to be able to find away to do this, most of strtok documentation is examples of using it to print the values out or storing into one variable using while loop. How can I do what I want to do?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <stdlib.h>

    const char  strDelimiter = ":"

    char*   p;
    char    str[] = "1:22:333:4444";
    int     one = 0, two = 0, three = 0, four = 0;

    if ( p = strtok(str, strDelimiter) )     { one = atoi(p); }
    if ( p = strtok(NULL, strDelimiter) )    { two = atoi(p); }
    if ( p = strtok(NULL, strDelimiter) )    { three = atoi(p); }
    if ( p = strtok(NULL, strDelimiter )     { four = atoi(p); }
« Last Edit: June 13, 2010, 10:58:23 am by lloyddean » Logged

Pages: [1]   Go Up
Jump to: