Share this page

# Example: strtok

In this example, strtok extracts the components of a file path. Multiple calls to strtok extract each successive component from the string, path.

The separators define how a token is demarcated i.e., they are used by strtok to find breaks between the tokens. The separators here are either `\' or `.'

When using multiple calls to strtok on the same string, specify the string only on the first invocation. Subsequent calls must pass NULL as the string parameter, as we do below.

        #include <string.h>
        char path[] = "c:\\mycomp\\lrun\\bin\\wlrun.exe";
        char separators[] = ".\\";
        char * token;

    // Get the first token

        token = (char *)strtok(path, separators);
        if (!token) {
            lr_output_message ("No tokens found in string!");
            return( -1 );
        }

    // While valid tokens are returned

        while (token != NULL ) {
            lr_output_message ("%s", token );

    // Get the next token

            token = (char *)strtok(NULL, separators);
        }

    Example: Output:
    Action.c(18): c:
    Action.c(18): mycomp
    Action.c(18): lrun
    Action.c(18): bin
    Action.c(18): wlrun
    Action.c(18): exe

Tips to improve your search results... VuGen Function Reference Version 25.1-25.3 Function Reference 25.1-25.3 All View Local Help Center View Online Help Center Version: