函数strtok过程时,两个字符串在同一时间 [英] strtok when process two strings at same time

查看:114
本文介绍了函数strtok过程时,两个字符串在同一时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新C和pretty困惑如何使用的strtok同时处理多个字符串,对于一个简单的例子,我想用的strtok提取号码,然后进行比较。

New in C and pretty confused about how to deal with several strings at the same time using strtok, for a simply example, I want to use strtok to extract the number and compare then.

#include <stdio.h>
#include <string.h>
int main()
{
   char s1[100]="11.54";
   char s2[100]="12.55";

   const char tok[2]=".";
   char* token1=strtok(s1,tok);
   char* token2=strtok(s2,tok);


while(token1 !=NULL && token2 !=NULL){
   int temp=strcmp(token1,token2);

   if(temp==0){
       token1=strtok(NULL,tok);
       token2=strtok(NULL,tok);

   }
   else if(temp<0){
     printf("%d\n",-1);
     return;
   } 
   else{
        printf("%d\n",1);
      return;
   }

}

if(token1 !=NULL){
  printf("%d\n",1);
  return; 
} 
if(token2 !=NULL){
   printf("%d\n",-1);
   return;
}

printf("%d\n",0);

return 0;
}

但是,当我使用的strtok的的strtok(NULL,令牌)将指向当前字符串,并会做这样的:11-> 12> 55-> NULL,跳过54

But when I use the strtok, the strtok(NULL,token)will point to the current string and will do like: 11->12>55->NULL and skip the 54

我怎么能处理这样的情况呢?谢谢!

How could I deal with such situation? Thanks!!

推荐答案

不要使用的strtok()。该文件会告诉你的strtok()是不可重入(即不应跨线程使用),但也许不太明显的事实是,它是不可重入的原因是因为它使用一个内部保存变量来记住它得。这意味着你也不能同时使用两个实例。而是使用 strtok_r()或做不到这一点 strsep()可能会奏效。

Do not use strtok(). The documentation will tell you strtok() is not reentrant (i.e. should not be used across threads), but perhaps less obvious is the fact that the reason it is not reentrant is because it uses an internal save variable to remember where it's got to. That means you also can't use two instances at once. Instead use strtok_r() or failing that strsep() might work.

strtok_r()就像 strtok的,保存你传递一个字符** (即一个指针的char * ),在那里可以保存在那里的得到了。

strtok_r() is just like strtok, save that you pass it a char ** (i.e. a pointer to char *) where it can save where it's got to.

借助 GNU libc的手册页给出了一个很好的例子使用嵌套的 strtok_r 这就是你正在尝试做的:

The GNU libc manual page gives a good example of using a nested strtok_r which is what you are trying to do:

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

   int
   main(int argc, char *argv[])
   {
       char *str1, *str2, *token, *subtoken;
       char *saveptr1, *saveptr2;
       int j;

       if (argc != 4) {
           fprintf(stderr, "Usage: %s string delim subdelim\n",
                   argv[0]);
           exit(EXIT_FAILURE);
       }

       for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
           token = strtok_r(str1, argv[2], &saveptr1);
           if (token == NULL)
               break;
           printf("%d: %s\n", j, token);

           for (str2 = token; ; str2 = NULL) {
               subtoken = strtok_r(str2, argv[3], &saveptr2);
               if (subtoken == NULL)
                   break;
               printf(" --> %s\n", subtoken);
           }
       }

       exit(EXIT_SUCCESS);
   }

这篇关于函数strtok过程时,两个字符串在同一时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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