Function Name: strtok
Function: searches for words separated by the delimiters specified in the second string.
Usage: char * strtok (char * str1, char * str2 );
Program Example:
# Include <string. h>
# Include <stdio. h>
Int main (void)
{
Char input [16] = "ABC, D ";
Char * P;
/* Strtok places a null Terminator
In front of the token, if found */
P = strtok (input ,",");
If (p) printf ("% s \ n", P );
/* A second call to strtok using a null
As the first parameter returns a pointer
To the character following the token */
P = strtok (null ,",");
If (p) printf ("% s \ n", P );
Return 0;
}
The functions with _ r mainly come from UNIX. The difference between all functions with and without _ r is that functions with and without _ r are thread-safe, and R means reentrant and reentrant.
The result of the above program running is
ABC
D
1. strtok Introduction
As we all know, strtok can be based on user-provided delimiters (and separators can also be plural numbers, such as ",").
Splits a string until "\ 0" is encountered ".
For example, separator = "," string = "Fred, John, Ann"
Using strtok, we can extract the three strings "Fred", "John", and "Ann.
C aboveCodeIs
Quote: int in = 0;
Char buffer [] = "Fred, John, Ann"
Char * P [3];
Char * buff = buffer;
While (P [in] = strtok (BUF ,","))! = NULL ){
I ++;
Buf = NULL ;}
As shown in the above Code, the first execution of strtok needs to take the address of the target string as the first parameter (BUF = buffer), and then strtok needs to take null as the first parameter (BUF = NULL ). The pointer column P [] stores the split result. P [0] = "John", P [1] = "John ", P [2] = "Ann", and Buf becomes Fred \ 0john \ 0ann \ 0.
2. strtok Vulnerabilities
Let's change our plan: We have a string named "Fred male 25, John male 62, Anna female 16". We want to sort this string and input it to a struct,
Quote: struct person {
Char [25] Name;
Char [6] sex;
Char [4] age;
}
To do this, one of the methods is to extract a string separated by commas (,) and then separate it with spaces.
For example, extract "Fred male 25" and split it into "Fred" "male" "25"
Below I wrote a small program to demonstrate this process:
Quote: # include <stdio. h>
# Include <string. h>
# Define info_max_sz 255
Int main ()
{
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;
}< br> P [IN ++] = "***"; // representation segmentation
Buf = NULL;}
Printf ("here we have % d strings \ n", I );
For (Int J = 0; j <in; j ++)
Printf ("> % S <\ n", P [J]);
Return 0;
}
The output of this program is:
Here we have 4 strings
> Fred <
> Male <
> 25 <
> *** <
This is just a small piece of data, not what we need. But why? This is because strtok uses a static (static) pointer to operate data. Let me analyze the running process of the above Code:
RedFor strtokPosition pointed by the built-in pointer,BlueFor strtokModify
1 ."FRed male 25, John male 62, Anna female 16 "// External Loop
2. "Fred male 25\ 0JOhn male 62, Anna female 16 "// enters the inner loop
3. "Fred\ 0MAle 25 \ 0 John male 62, Anna female 16"
4. "Fred \ 0 male\ 025 \ 0 John male 62, Anna female 16"
5 "Fred \ 0male \ 025\ 0John male 62, Anna female 16 "// when an inner loop Encounters" \ 0 ", it returns to the outer loop.
6 "Fred \ 0male \ 025\ 0John male 62, Anna female 16 "// an External Loop Encounters" \ 0.
3. Use strtok_r
In this case, we should use strtok_r, strtok reentrant.
Char * strtok_r (char * s, const char * delim, char ** ptrptr );
Compared with strtok, we need to provide a pointer for strtok to operate, instead of using a matched pointer like strtok.
Code:
Quote: # include <stdio. h>
# Include <string. h>
# Define info_max_sz 255
Int main ()
{
Int in = 0;
Char buffer [info_max_sz] = "Fred male 25, John male 62, Anna female 16 ";
Char * P [20];
Char * Buf = buffer;
Char * outer_ptr = NULL;
Char * inner_ptr = NULL;
while (P [in] = strtok_r (BUF, ",", & outer_ptr ))! = NULL) {
Buf = P [in];
while (P [in] = strtok_r (BUF, "", & inner_ptr ))! = NULL) {
In ++;
Buf = NULL;
}< br> P [IN ++] = "***";
Buf = NULL ;}
Printf ("here we have % d strings \ n", I );
For (Int J = 0; JN <I; j ++)
Printf ("> % S <\ n", P [J]);
Return 0;
}
The output for this time is:
Here we have 12 strings
> Fred <
> Male <
> 25 <
> *** <
> JOHN <
> Male <
> 62 <
> *** <
> Anna <
> Female <
> 16 <
> *** <
Let me analyze the running process of the above Code:
RedFor strtok_rPosition pointed to by outer_ptr,
PurpleFor strtok_rPosition pointed to by inner_ptr,
BlueFor strtokModify
1 ."FRed male 25, John male 62, Anna female 16 "// External Loop
2 ."FRed male 25\ 0JOhn male 62, Anna female 16 "// enters the inner loop
3. "Fred\ 0MAle 25 \ 0JOhn male 62, Anna female 16"
4 "Fred \ 0 male\ 025 \ 0JOhn male 62, Anna female 16"
5 "Fred \ 0male \ 025\ 0JOhn male 62, Anna female 16 "// when the inner loop Encounters" \ 0 ", it returns to the outer loop.
6 "Fred \ 0male \ 025 \ 0JOhn male 62\ 0ANNA female 16 "// enters the inner loop
Note:
Reprinted
ArticleReprinted connection: http://blog.chinaunix.net/u2/66402/showart_1168731.html