Go Down

Topic: strtok help (Read 773 times) previous topic - next topic

Vostro

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?

lloyddean

#1
Jun 13, 2010, 04:02 am Last Edit: Jun 13, 2010, 05:58 pm by lloyddean Reason: 1
Code: [Select]

#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); }

Go Up