C中的嵌套strtok函数问题 [英] Nested strtok function problem in C

查看:20
本文介绍了C中的嵌套strtok函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

I have a string like this:

a;b;c;d;e
f;g;h;i;j
1;2;3;4;5

我想逐个元素解析它.我使用了嵌套的 strtok 函数,但它只是拆分了第一行并使令牌指针为空.我怎样才能克服这个?代码如下:

and i want to parse it element by element. I used nested strtok function but it just splits first line and makes null the token pointer. How can i overcome this? Here is the code:

token = strtok(str, "
");

while(token != NULL && *token != EOF)
{
    char a[128], b[128];
    strcpy(a,token);
    strcpy(b,a);
    printf("a:%s
",a);
    char *token2 = strtok(a,";");
    while(token2 != NULL)
    {
        printf("token2 %s
",token2);
        token2 = strtok(NULL,";");
    }
    strcpy(token,b);
    token = strtok(NULL, "
");
    if(token == NULL)
    {
        printf("its null");
    }
}

输出:

token 2 a
token 2 b
token 2 c
token 2 d
token 2 e

推荐答案

你不能用 strtok();使用 strtok_r() 来自 POSIX 或 <来自 Microsoft 的 href="https://msdn.microsoft.com/en-us/library/ftsafwz3.aspx" rel="noreferrer">strtok_s()(如果可用),或重新考虑您的设计.

You cannot do that with strtok(); use strtok_r() from POSIX or strtok_s() from Microsoft if they are available, or rethink your design.

char *strtok_r(char *restrict s, const char *restrict sep,
               char **restrict lasts);
char *strtok_s(char *strToken, const char *strDelimit, char **context); 

这两个函数可以互换.

注意一个变种 strtok_s() 在 C11 的可选部分中指定(ISO/IEC 9899:2011 中的附录 K).但是,除了 Microsoft 之外,很少有供应商实现了该部分标准中的接口.附件 K 中指定的 strtok_s() 版本与 Microsoft 的 strtok_s() 具有不同的接口——类似的问题困扰着附件 K 中指定的许多其他函数.>

使用 strtok_r()

Note that a variant strtok_s() is specified in an optional part of C11 (Annex K in ISO/IEC 9899:2011). However, few suppliers other than Microsoft have implemented the interfaces in that section of the standard. The version of strtok_s() specified in Annex K has a different interface from Microsoft's strtok_s() — similar problems bedevil a number of the other functions specified in Annex K.

#include <string.h>
#include <stdio.h>

int main(void)
{
    char str[] = "a;b;c;d;e
f;g;h;i;j
1;2;3;4;5
";
    char *end_str;
    char *token = strtok_r(str, "
", &end_str);

    while (token != NULL)
    {
        char *end_token;
        printf("a = %s
", token);
        char *token2 = strtok_r(token, ";", &end_token);
        while (token2 != NULL)
        {
            printf("b = %s
", token2);
            token2 = strtok_r(NULL, ";", &end_token);
        }
        token = strtok_r(NULL, "
", &end_str);
    }

    return 0;
}

结果

a = a;b;c;d;e
b = a
b = b
b = c
b = d
b = e
a = f;g;h;i;j
b = f
b = g
b = h
b = i
b = j
a = 1;2;3;4;5
b = 1
b = 2
b = 3
b = 4
b = 5

没有 strtok_r()

这适用于上下文 - 前提是数据以换行符结尾.

Without strtok_r()

This works in context - provided that the data ends with a newline.

#include <string.h>
#include <stdio.h>

int main(void)
{
    char data[] = "a;b;c;d;e
f;g;h;i;j
1;2;3;4;5
";
    char *string = data;
    char *token  = strchr(string, '
');

    while (token != NULL)
    {
        /* String to scan is in string..token */
        *token++ = '';
        printf("a = %s
", string);
        char *token2 = strtok(string, ";");
        while (token2 != NULL)
        {
            printf("b = %s
", token2);
            token2 = strtok(NULL, ";");
        }
        string = token;
        token = strchr(string, '
');
    }

    return 0;
}

输出

a = a;b;c;d;e
b = a
b = b
b = c
b = d
b = e
a = f;g;h;i;j
b = f
b = g
b = h
b = i
b = j
a = 1;2;3;4;5
b = 1
b = 2
b = 3
b = 4
b = 5

这篇关于C中的嵌套strtok函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆