Menu
Is free
check in
the main  /  Problems / Strtok s C Description. MAN STRTOK_R (3): Extraction of elements (tokens) from the string

Strtok s C Description. MAN STRTOK_R (3): Extraction of elements (tokens) from the string

4 response

Two things you need to know about Strtok. As mentioned, he "supports the inner state." In addition, he spoiled the string that you feed it. Essentially, he will write "\\ 0", where he will find the marker that you have provided, and will return the pointer to the beginning of the line. Internally, it supports the location of the last tokeny; And the next time you call it, he will start from there.

An important consequence is that you cannot use STRTOK for a line type Const Char * "Hello World"; Since you will receive access violation when changing the contents of the Const Char * string.

The "good thing" in Strtok is that in fact it does not copy the lines, so you do not need to control the additional memory allocation, etc. But if you do not understand the foregoing, you will have problems with its use.

Example. If you have "this, there is a string", the serial calls STRTOK will generate pointers as follows (value ^ is the return value). Please note that the "\\ 0" is added, where the tokens are found; This means that the original string is changed:

T his, is, a, string \\ 0 this, is, a, string this \\ 0 is, a, string \\ 0 this ^ this \\ 0 is \\ 0 a, string \\ 0 is ^ this \\ 0 is \\ 0 a \\ Hope it makes sense.

The STRTOK () function stores data between calls. It uses this data when you call it using the NULL pointer.

The point in which the last token was found is stored inside using a function that will be used next to the following call (a specific library is required to prevent data failures).

Strtok supports internal state. When you call it with non-NULL, it re-initializes yourself to use the string you deliver. When you call it with NULL, it uses this line and any other state it currently gets to return the following tokens.

Due to how Strtok works, you need to make sure that you connect with a multi-threaded version of C fulfillment if you write multi-threaded application. This ensures that each thread will receive their own internal state for STRTOK.

Strtok function stores data in an internal static variable, which is distributed between all threads.

To ensure security security, you must use STRTOK_R

Take a look at Static Char * Last;

Char * Strtok (S, Delim) Register Char * S; REGISTER CONST CHAR * DELIM; (REGISTER CHAR * SPANP; REGISTER INT C, SC; CHAR * TOK; STATIC CHAR * LAST; if (s \u003d\u003d null && (s \u003d last) \u003d\u003d null) Return (NULL); / * * SKIP (span) Leading Delimiters (S + \u003d STRSPN (S, DELIM), SORT OF). * / CONT: C \u003d * S ++; FOR (SPANP \u003d (char *) DELIM; (SC \u003d * SPANP ++)! \u003d 0;) (if (C \u003d\u003d SC) Goto CONT;) if (C \u003d\u003d 0) (/ * no non-delimiter characters * / last \u003d null; return (null);) Tok \u003d S - 1; / * * Scan Token (Scan for Delimiters : S + \u003d STRCSPN (S, DELIM), SORT OF). * NOTE THAT DELIM MUST HAVE ONE NUL; WE STOP IF WE See That, Too. * / for (;;) (C \u003d * S ++; SPANP \u003d (CHAR *) DELIM; DO (if ((SC \u003d * SPANP ++) \u003d\u003d C) (IF (C \u003d\u003d 0) S \u003d NULL; ELSE S [-1] \u003d 0; Last \u003d S; Return (Tok);)) While (SC! \u003d 0);) / * NotReached * /)

Share

#Include. Char * Strtok (Char * str1, Const char * sTR2.);

The STRTOK () function returns the pointer to the next lex in the string addressing the parameter str1. Symbols forming a string addressable by the parameter sTR2.are dividers that define a lex. In the absence of a lexeme to be returned, a zero pointer is returned.

In the C99 version to the parameters str1 and sTR2. RESTRICT qualifier applied.

To divide some string on the lexemes, when the STRTOK function is first calling the parameter str1 Must point to the beginning of this line. Upon subsequent calls function as a parameter str1 You need to use a zero pointer. This way the entire line is broken down on the lexemes.

Each time you contact the STRTOK () function, you can use different sets of separators.

Example

This program breaks the string "the grass green, the sun shines" on the lexemes whose separators serve spaces and commas. As a result, it turns out

Hestern | Green | Sun | Blusters #include #Include. INT MAIN (Void) (char * p; p \u003d strtok ("Green grass, shine", ""); printf (p); do (p \u003d strtok ("\\ 0", ","); if (p ) Printf ("|% s", p);) while (P); Return 0;)

Syntax:

#Include.
char * STRTOK (Char * Str, Const Char * SEP);

Arguments:

str is a pointer to a broken string.
SEP is a pointer to a string containing a set of separator characters.

Return value:

NULL - If the string STR cannot be divided into parts.
Pointer to the first character of the selected part of the line.

Description:

The STRTOK feature highlights the next part of the line to which the STR argument is separated from one of the characters of the separators specified in the string to which the SEP argument indicates. The serial call of the STRTOK function leads to the breakdown of the string string on the part (lexemes).

"When you first call the STRTOK function, the start of the shared string (STR) and the beginning of a string containing separators (SEP) is specified. At the beginning, the STRTOK function alternately views the string strings string and searches for a symbol that is not contained in the SEP separators string. If in the string string, the string end symbol is encountered earlier than the symbol is not included in the SEP string, it is impossible to divide the string of STR on the part, the zero pointer (NULL) is returned. If such a symbol is found, it is considered the beginning of the first part of the string str. "

Next, the STRTOK function is looking for a separator, that is, a symbol included in the SEP string. If such a symbol is not found, it is believed that the string STR consists of one part and the subsequent separation of the string string will return the zero pointer. If such a symbol is found. It is replaced with a zero symbol (the end of the line symbol). Next, the STRTOK feature remembers the current position (the pointer to the character from which the search for the next part of the line will begin) and returns the pointer to the beginning of the first selected part of the line.

If the STRTOK function returned a non-zero pointer, you can continue the partition of the string string on the part. To continue splitting the line, the STRTOK function is re-called, but instead of the pointer to the broken line, NULL is specified as the first augment. In this case, the STRTOK function will continue to break from the memorized address. The algorithm of partition will remain the same.

Example:

In the example, the "Test1 / Test2 / Test3 / Test4" line is divided into parts by the "/" separator using the Strtok function. The result of partition is displayed on the console.

Result:

Conclusion in the console:


cHAR FAR * FAR _FSTRTOK (Const Char Far * Str1, Const Char Far * Str2)

Description:

The STRTOK () function returns a pointer to the next lex in the string to which STR1 indicates. Symbols from the string to which STR2 indicates are used as limiters that determine the lex. If the lexema is not found, NULL is returned.

During the first call of the STRTOK () function, STR1 is actually used as a pointer. With subsequent challenges, NULL is used as the first argument. Thus, the entire string can be broken on the lexemes.

It is important to understand that the STRTOK () function modifies the string to which STR1 indicates. Every time a lexeme was found, on the spot where the limiter was found, a zero symbol is placed. Thus, STRTOK () is moving along the string.

Each time the STRTOK () call, you can vary the set of limiters.

The _fstrtok () function is the Far version of the function in question.

The following program breaks the "The Summer Solidier, The Sunshine Patriot" string using spaces and commas as limiters. As a result of the program, the program will be formed the line of the following type: "The | Summer | Soldier | The | Sunshine | Patriot. "
#Include.
#Include.
iNT MAIN (Void)
{
char * p;
P \u003d STRTOK ( "The Summer Soldier, The Sunshine Patriot", " " ) ;
printF (P);
do (
P \u003d STRTOK (" \0 " , ", " ) ;
if (p) Printf ("|% s", p);
) While (P);
return 0;
}

Other Alias.

Strtok.

OVERVIEW

#Include.

char * Strtok (Char *str., Const char *delim.);
char * Strtok_R (char *str., Const char *delim., char **saveptr.);

Property Testing Macro Requirements for GLIBC (see feature_Test_Macros.(7)):

strtok_r.(): _Svid_source || _BSD_SOURCE || _Posix_c_source\u003e \u003d 1 || _Xopen_source || _Posix_source.

DESCRIPTION

Function strtok.() separates the string to a sequence of zero or more non-empty tokens. Under the first call strtok.() The analyzed string must be specified in the argument. str.. In each subsequent call, which analyzes the same string, value str. There must be null.

In argument delim. A set of bytes is set, which are considered token separators in the row analyzed. The caller may indicate different lines in delim. In subsequent challenges when analyzing the same string.

Each call strtok.() Returns a pointer to a string ending with NULL, which contains the following token. This line does not include byte separator. If there are no more tokens, then strtok.() Returns NULL.

Call sequence strtok.(), operating one line, supports a pointer that determines the point from which the search for the next token begins. First challenge strtok.() Appoints this pointer to the first byte line. The beginning of the next token is determined by the search forward in str. The next byte is not separator. If the byte is found, it is taken as the beginning of the next token. If such a byte is not found, there are no more tokens and strtok.() Returns NULL (for an empty string or consisting only of separators in this case, NULL will return when the first call strtok.()).

The end of each tokeny is a search for ahead, which is found until the byte-separator is found or the final byte NULL ("\\ 0"). If the byte-separator is found, it is replaced by a NULL byte to complete the current tokens, and strtok.() Keeps a pointer to the next byte; This pointer will be used as an initial point when searching for the next token. In this case strtok.() Returns the pointer to the beginning of the found tokeny.

From the description above it follows that the sequence of two or more continuous separators bytes in the view viewed is considered one separator, and the dividers bytes at the beginning or end of the row are ignored. In other words, tokens returned strtok.() - Always not empty lines. That is, for example, if there is a string " aAA ;; BBB,", Then subsequent challenges strtok.() with specified lines dividers ;, "Would returned the lines" aAA"And" bBB.", And then the NULL pointer.

Function strtok_r.() is a reenterable version strtok.(). Argument saveptr. is a pointer to the variable char *used inside strtok_r.() To account for the context between subsequent challenges when analyzing the same line.

Under the first call strtok_r.() Value str. should indicate the analyzed string, and the value saveptr. ignored. Upon subsequent calls the value str. There must be , and the value saveptr. should not change from the moment of the previous call.

At the same time, different lines can be analyzed at multiple launches. strtok_r.() with various arguments saveptr..

Return value

Functions strtok.() I. strtok_r.() Return the pointer to the next token or , if there are no more tokens.

Attributes

Description of the terms of this section, see attributes.(7).
Interface Attribute Value
strtok.() ensure in the threadsinsecure (MT-Unsafe Race: Strtok)
strtok_r.() ensure in the threadsharmless (MT-Safe)

COMPLIANCE

strtok.() POSIX.1-2001, POSIX.1-2008, C89, C99, SVR4, 4.3BSD. strtok_r.() POSIX.1-2001, POSIX.1-2008.

Defects

Use these functions with caution. Take into account that: * These functions change their first argument. * These functions cannot be used with constant strings. * The identity of the byte separator is lost. * When analyzing the function strtok.() Uses static buffer, so it is not safe for the threads. Use strtok_r.() in this case.

EXAMPLE

The program presented below uses nested cycles that cause strtok_r.() To split the string to the components of its tokens. In the first parameter command line The analyzed string is set. In the second parameter, the byte (s) is a separator that is used to divide the string to "composite" tokens. The third parameter indicates bytes (s) - the separator, which is used to separate "composite" tokens on the fertile.

Example of the results of the program's output:

$ ./a.out "A / BBB /// CC; XXX: yyy:" ":;" "/" 1: A / BBB /// CC -\u003e A -\u003e BBB -\u003e CC 2: XXX -\u003e XXX 3: YYY -\u003e YYY

Source code program

#Include. #Include. #Include. Int Main (int argc, char * argv) (char * str1, * str2, * token, * subtoken; char * saveptr1, * saveptr2; int j; if (argc! \u003d 4) (FPRINTF (STDERR, "use:% s String Delim Subdelim \\ n ", argv); exit (exit_failure);) for (j \u003d 1, str1 \u003d argv ;; j ++, str1 \u003d null) (token \u003d strtok_r (STR1, ARGV, & SAVEPTR1); if (token \u003d \u003d NULL) break; printf ("% d:% s \\ n", j, token); for (str2 \u003d token ;; STR2 \u003d NULL) (subtoken \u003d strtok_r (STR2, ARGV, & Saveptr2); if (subtoken \u003d\u003d Null) break; printf ("-\u003e% s \\ n", subtoken);)) exit (exit_success);)

Another example of a program that uses strtok.() can be found in getAddrinfo_A.(3).