Chinaunix首页 | 论坛 | 博客
  • 博客访问: 826903
  • 博文数量: 109
  • 博客积分: 650
  • 博客等级: 上士
  • 技术积分: 1483
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-01 17:29
文章分类

全部博文(109)

文章存档

2016年(5)

2015年(21)

2014年(16)

2013年(38)

2012年(29)

分类: LINUX

2013-04-10 16:12:29


点击(此处)折叠或打开

  1. CSE:~/test # cat strtok.c
  2. #include <stdio.h>
  3. #include <string.h>

  4. int main(void)
  5. {
  6. char str[] = "root:x:0:root:/root:/bin/sh:";
  7. char *token;
  8. token = strtok(str,":");
  9. printf("%s\n",token);
  10. while ((token = strtok(NULL,":")) != NULL)
  11. printf("%s\n",token);
  12. return 0;
  13. }
  14. CSE:~/test # ./strtok
  15. root
  16. x
  17. 0
  18. root
  19. /root
  20. /bin/sh
第一次调用时把字符串传给strtok,以后每次调用时第一个参数只要传NULL就可以了,strtok函数自己会记住上次处理到字符串的什么位置(显然这是通过strtok函数中的一个静态指针变量记住的)

点击(此处)折叠或打开

  1. (gdb) start
  2. Temporary breakpoint 1 at 0x400594: file strtok.c, line 6.
  3. Starting program: /root/test/strtok
  4. Missing separate debuginfo for /lib64/ld-linux-x86-64.so.2
  5. Try: zypper install -C "debuginfo(build-id)=c1807b5762068e6c5f4a6a0ed48d9d4469965351"
  6. Missing separate debuginfo for /lib64/libc.so.6
  7. Try: zypper install -C "debuginfo(build-id)=74ef01bbffa60bc29e6768832b775b13c191a60b"

  8. Temporary breakpoint 1, main () at strtok.c:6
  9. 6 char str[] = "root:x:0:root:/root:/bin/sh:";
  10. (gdb) n
  11. 8 token = strtok(str,":");
  12. (gdb) display str
  13. 1: str = "root:x:0:root:/root:/bin/sh:"
  14. (gdb) n
  15. 9 printf("%s\n",token);
  16. 1: str = "root\000x:0:root:/root:/bin/sh:"
  17. (gdb)
  18. root
  19. 10 while ((token = strtok(NULL,":")) != NULL)
  20. 1: str = "root\000x:0:root:/root:/bin/sh:"
  21. (gdb)
  22. 11 printf("%s\n",token);
  23. 1: str = "root\000x\000\060:root:/root:/bin/sh:"
  24. (gdb)
  25. x
  26. 10 while ((token = strtok(NULL,":")) != NULL)
  27. 1: str = "root\000x\000\060:root:/root:/bin/sh:"
  28. (gdb)
  29. 11 printf("%s\n",token);
  30. 1: str = "root\000x\000\060\000root:/root:/bin/sh:"
  31. (gdb)



阅读(1166) | 评论(0) | 转发(0) |
0

上一篇:C++ cin问题

下一篇:链表的实现(C++)

给主人留下些什么吧!~~