On the main points and realization principle of function strtok and Strtok_r (II.)

Source: Internet
Author: User
Tags strtok

Turn from: http://blog.csdn.net/helpxs/article/details/6959057

(i) has introduced the use of strtok functions of some considerations, this article will introduce a strtok application and draw out the Strtok_r function.

1. An application example

A classic example on the web is to split the string into the structure. For example, an existing structural body

typedef struct person{
Char name[25];
Char sex[10];
Char age[4];
}person;

Required from string char buffer[info_max_sz]= "Fred male 25,john male 62,anna female 16"; The name, gender and age were extracted.

One possible idea is to set up a two-tier loop. The outer loop, first "," (comma) as the delimiter, the information of three separate, and then for each substring, and then the "(space) for the demarcation of the name, sex and age."

According to this idea, should be able to achieve the desired function. To simplify the procedure, we call Strtok, first saving the substring to a string pointer array, and printing all the substrings stored in the pointer array at the end of the program, verifying the correctness of the program. The procedures to be obtained should be as follows:

View plain Copy to clipboard int in=0;   char buffer[info_max_sz]= "Fred male 25,john  male 62,anna female 16 ";       char *p[20];   char *buf = buffer;   while (P[in]=strtok (buf, ",")!=null)     {        buf=p[in];       while (P[in]=strtok (buf, " ") !=null)         {            in++;           buf=NULL;        }       buf=NULL;  }   printf ("here we have %d  strings/n ",  in);   for  (int j=0; j<in; j++)    {           printf (">%s</n", P[j]);  }   [CPP]   View plain copy int in=0;   char buffer[info_max_sz]= "fred male 25,john  Male 62,anna female 16 ";       char *p[20];   Char  *buf = buffer;   while (P[in]=strtok (buf, ",")!=null)     {       buf=p[in];       while (P[in]=strtok (buf, " ")!=null )         {           in++;            buf=NULL;       }        buf=NULL;  }   printf ("here we have %d  strings/n ",  in);   for  (int j=0; j<in; j++)    {           printf (">%s</n", P[j]);  }  

The result of the execution is that only the first person's information is extracted. It seems that the implementation of the procedure is not what we expected. what is the reason.

The reason: in the first outer loop , strtok the comma after "Fred male 25," to the '/0 ', when the this pointer inside the strtok points to the latter character ' J 'of the comma. after the first internal cycle , the "Fred" "Male" "25" were extracted. After "25" is fetched, the this pointer inside the function is modified to point to '/0 ' after "25". after the inner loop is finished (the inner loop actually executes 4 times), the second outer loop begins , because the first parameter of the function is set to null,strtok the position to which the this pointer points to as the starting position of the decomposition. Unfortunately, the this pointer is pointing to '/0 ', strtok cannot be split on an empty string, and returns NULL. The outer loop ends. So, we only get the first person's information as shown in the picture.

It seems that using strtok can not be used to solve the problem of extracting multiple people's information through two-tier loops. is there any other way? Obviously, there are other ways.

I have given a solution. At the same time, with ', ' (comma) and ' (space) as the delimiter, a layer of loops to solve the problem.

View plain Copy to Clipboard in = 0; while ((P[in] = Strtok (buf, ","))!= NULL)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.