C中嵌套的strtok函数问题

时间:2011-01-14 17:17:51

标签: c nested token tokenize strtok

我有一个这样的字符串:

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

我想逐个元素地解析它。我使用嵌套的strtok函数,但它只是拆分第一行并使令牌指针为null。我怎么能克服这个?这是代码:

token = strtok(str, "\n");

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

输出:

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

2 个答案:

答案 0 :(得分:25)

strtok()你无法做到这一点;使用来自POSIX的strtok_r()或来自Microsoft的strtok_s()(如果可用),或重新考虑您的设计。

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之外,很少有供应商在该部分标准中实现了接口。

使用strtok_r()

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

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

    while (token != NULL)
    {
        char *end_token;
        printf("a = %s\n", token);
        char *token2 = strtok_r(token, ";", &end_token);
        while (token2 != NULL)
        {
            printf("b = %s\n", token2);
            token2 = strtok_r(NULL, ";", &end_token);
        }
        token = strtok_r(NULL, "\n", &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()

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

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

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

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

    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

答案 1 :(得分:4)

strtok_r是最好,最安全的解决方案,但还有一种方法可以使用strtok

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

int main ()
{
  char str[] = "a;b;c;d;e\nf;g;h;i;j\n1;2;3;4;5\n";
  char *line;
  char *token;
  char buf[256];

  for (line = strtok (str, "\n"); line != NULL;
       line = strtok (line + strlen (line) + 1, "\n"))
    {
      strncpy (buf, line, sizeof (buf));
      printf ("Line: %s\n", buf);
      for (token = strtok (buf, ";"); token != NULL;
       token = strtok (token + strlen (token) + 1, ";"))
    {
      printf ("\tToken: %s\n", token);
    }
    }

  return 0;
}

输出:

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