Strtok, strtok_s, strtok_r string segmentation functions

Source: Internet
Author: User
Tags strtok

1. strtok function function prototype: char * strtok (char *str, const char * delimiters); parameters: str, string to be split (c-string); delimiters, delimiter string. How to use:This function is used to split a string into fragments. The parameter str points to the string to be split, and the parameter delimiters all the characters contained in the split string. When Strtok () finds the split character contained in the parameter delimiters in a string of parameter s, the character is changed to the \ s character. At the first call, Strtok () must give the parameter S string, and the subsequent call sets the parameter s to null. Each successful call returns a pointer to the segmented fragment. principle:Acts on the string str, which is delimited by the characters contained in delimiters and divides str into a substring; if s is null, the function holds the pointer save_ptr in the next call as the starting position. Note:1. When you use this function for string segmentation, the integrity of the decomposed string is broken, and the pre-and post-invocation s are different. After the first split, the original string str is the first string after the partition is completed,The remaining string is stored in a static variable save_ptr, so when multithreading accesses the static variable at the same time, an error occurs. 2.delimiters contains multiple characters    : The function definition of strtok in the GNU C library: "Parse S into tokens separated by characters in DELIM ". This means that the characters contained in the Delim can be used as delimiters, rather than strictly matched. Delim can be understood as a collection of separators . This is very important. 3. If the first character is a split character, it will be omitted automatically.
//crt_strtok.c//compile with:/w3//in the program, a loop uses strtok//To print all the tokens (separated by commas//or blanks) in the string named "string".//#include <string.h>#include<stdio.h>Char string[] ="A String\tof,, tokens\nand some more tokens";CharSeps[] =", \t\n";// characters ', ' \ t ' \ n ' are used as split characters Char*token;intMainvoid) {printf ("tokens:\n" ); //establish string and get the first token:token = Strtok (string, SEPs);//C4996//Note:strtok is deprecated; Consider using strtok_s instead    while(Token! =NULL) {      //While there is tokens in "string"printf"%s\n", token); //Get Next token:token = Strtok (NULL, SEPs);//C4996   }}

Results:

String0

2, strtok_s function strtok_s is a partition string security function under Windows, its function prototype is as follows: Char *strtok_s (char *strtoken, const char *strdelimit, char **buf); This function stores the remaining strings in the BUF variable instead of the static variable, thus guaranteeing security. 3, Strtok_r function strtok_s function is the security function of the partition string in Linux, the function is declared as follows: Char *strtok_r (char *str, const char *delim, char **saveptr); The function also destroys the integrity of the decomposed string, but it preserves the remaining strings in the SAVEPTR variable, guaranteeing security.

Strtok, strtok_s, strtok_r string segmentation functions

Related Article

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.