LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1125|回复: 7

ANSI/POSIC 函数strtok

[复制链接]
发表于 2006-8-3 11:21:38 | 显示全部楼层 |阅读模式
Test case 1:

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

  3. int main ()
  4. {
  5.   char str[] ="This is a sample string, just testing.";
  6.   char * pch;
  7.   printf ("Splitting string "%s" in tokens:\n",str);
  8.   pch = strtok (str,",");   //精确定义一个截断符","
  9.   while (pch != NULL)
  10.   {
  11.     printf ("%s\n",pch);
  12.     pch = strtok (NULL, " ");
  13.   }
  14.   return 0;
  15. }
复制代码

Result :

  1. [root@root ANSI]# ./a.out
  2. Splitting string "This is a sample string, just testing." in tokens:
  3. This is a sample string
  4. just
  5. testing.
  6. [root@root ANSI]#
复制代码

Test case 2:

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

  3. int main ()
  4. {
  5.   char str[] ="This is a sample string, just testing.";
  6.   char * pch;
  7.   printf ("Splitting string "%s" in tokens:\n",str);
  8.   pch = strtok (str," ,");  //截断符","前面加上一个空格
  9.   while (pch != NULL)
  10.   {
  11.     printf ("%s\n",pch);
  12.     pch = strtok (NULL, " ");
  13.   }
  14.   return 0;
  15. }
复制代码

Result:

  1. [root@root ANSI]# ./a.out
  2. Splitting string "This is a sample string, just testing." in tokens:
  3. This
  4. is
  5. a
  6. sample
  7. string,
  8. just
  9. testing.
  10. [root@root ANSI]#
复制代码

例2中为什么没有用","作为截断符呢?
发表于 2006-8-3 12:03:26 | 显示全部楼层
manpage里说了
strtok()的第二个参数是截断符集合,也就是说字符串里的每一个字符都是截断符
回复 支持 反对

使用道具 举报

发表于 2006-8-8 01:30:25 | 显示全部楼层
1 LZ的问题可以man strtok
2 是POSIX,不是POSIC……
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-8 11:29:01 | 显示全部楼层
Post by ralph_cheng
manpage里说了
strtok()的第二个参数是截断符集合,也就是说字符串里的每一个字符都是截断符

如果说是以字符串中的每个字符为一个截断字符集, 那么上面的输出为什么还有:
string,
这样的输出呢? 截断字符","肯定不会在出现的。
回复 支持 反对

使用道具 举报

发表于 2006-8-8 14:53:26 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <string.h>
  3. int main ()
  4. {
  5.   char str[] ="This is a sample string, just testing.";
  6.   char * pch;
  7.   printf ("Splitting string "%s" in tokens:\n",str);
  8.   pch = strtok (str," ,");  //截断符","前面加上一个空格
  9.   while (pch != NULL)
  10.   {
  11.     printf ("%s\n",pch);
  12.     [color=red]pch = strtok (NULL, " ");[/color]==>pch = strtok (NULL, " ,");
  13.   }
  14.   return 0;
  15. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2006-8-9 04:44:08 | 显示全部楼层
Post by sybaselu
如果说是以字符串中的每个字符为一个截断字符集, 那么上面的输出为什么还有:
string,
这样的输出呢? 截断字符","肯定不会在出现的。

Before the while loop starts, str is splited into "This" and "is a ..." since the  " " between "This" and "is a ..." is a delimiter.

In the loop, "," is no longer a delimiter only " " is considered as a delimitor. Thats why "string," shows up.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-9 18:01:54 | 显示全部楼层
对于zlbruse:
  我的意思并不是要用","作为截断符,而是想知道在第一次调用strtok时为什么不用参数中多个字符作为截断符集,而只是以" "作为截断符,忽略了","。
To strong man ralph_cheng:
Tks, Owe, I dont't  think that I maybe obtain a little  hits from your complaintation. but I seem still be stuck in wh

at  your a bit puzzle saying. Well, since strtok() be called at first, the pointer pch pointed the return character of

strtok, that is , an character of "T..", becase the secnod parameter as "" , then the delimiter "." be omited. after e

ntring the loop, we have made the second calling of strtok() , so the delimiter "," no longer in the set of delimiter.

right?
回复 支持 反对

使用道具 举报

发表于 2006-8-9 22:49:10 | 显示全部楼层
Post by sybaselu
对于zlbruse:
  我的意思并不是要用","作为截断符,而是想知道在第一次调用strtok时为什么不用参数中多个字符作为截断符集,而只是以" "作为截断符,忽略了","。
To strong man ralph_cheng:
Tks, Owe, I dont't  think that I maybe obtain a little  hits from your complaintation. but I seem still be stuck in wh

at  your a bit puzzle saying. Well, since strtok() be called at first, the pointer pch pointed the return character of

strtok, that is , an character of "T..", becase the secnod parameter as "" , then the delimiter "." be omited. after e

ntring the loop, we have made the second calling of strtok() , so the delimiter "," no longer in the set of delimiter.

right?

在你的有问题的那个程序里,
第一个调用strtok是在while loop之前,截断符为空格和逗号,这次调用返回了第一个token,也就是"This",它被一个空格截断。
然后就开始while loop了,在loop里的strtok就不再把逗号当作截断符,而只把空格当作截断符(因为你是这样写的),所以逗号会出现在token里。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表