Jump to content
43oh

strtok_r Problem


Recommended Posts

Hi,
a simple program and also a simple problem, but I can not find a solution.
someone has a tip for me?


char text[] = "Value1, 1000, Value2,100,Value3,1.414,end";
void setup()
{
  
  Serial.begin(9600);
  
}

void loop(){
  
  char *p = text;
  char *str;
  
  
while ((str = strtok_r(p, ",", &p)) != NULL) // delimiter is comma
 {   Serial.println(str);
 delay(500);
 }


}

The output ist not as expected:

 

Value1

 1000
 Value2
100
Value3
1.414
end
Value1
Value1
Value1
Value1
Value1
Value1
Value1
.
.
.
.
.
 
 
I want this:
 
Value1
 1000
 Value2
100
Value3
1.414
end
Value1
 1000
 Value2
100
Value3
1.414
end
Value1
 1000
 Value2
100
Value3
1.414
end
.
.
.
 

 

Link to post
Share on other sites

char *strtok_r(char *str, const char *delim, char **saveptr);

On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

  char *p = text;
  char *q;
  char *str;

  while ((str = strtok_r(p, ",", &q)) != NULL) // delimiter is comma
  {
    p = NULL;
    Serial.println(str);
    delay(500);
  }
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...