-
CSE:~/test # cat strtok.c
-
#include <stdio.h>
-
#include <string.h>
-
-
int main(void)
-
{
-
char str[] = "root:x:0:root:/root:/bin/sh:";
-
char *token;
-
token = strtok(str,":");
-
printf("%s\n",token);
-
while ((token = strtok(NULL,":")) != NULL)
-
printf("%s\n",token);
-
return 0;
-
}
-
CSE:~/test # ./strtok
-
root
-
x
-
0
-
root
-
/root
-
/bin/sh
第一次调用时把字符串传给strtok,以后每次调用时第一个参数只要传NULL就可以了,strtok函数自己会记住上次处理到字符串的什么位置(显然这是通过strtok函数中的一个静态指针变量记住的)
-
(gdb) start
-
Temporary breakpoint 1 at 0x400594: file strtok.c, line 6.
-
Starting program: /root/test/strtok
-
Missing separate debuginfo for /lib64/ld-linux-x86-64.so.2
-
Try: zypper install -C "debuginfo(build-id)=c1807b5762068e6c5f4a6a0ed48d9d4469965351"
-
Missing separate debuginfo for /lib64/libc.so.6
-
Try: zypper install -C "debuginfo(build-id)=74ef01bbffa60bc29e6768832b775b13c191a60b"
-
-
Temporary breakpoint 1, main () at strtok.c:6
-
6 char str[] = "root:x:0:root:/root:/bin/sh:";
-
(gdb) n
-
8 token = strtok(str,":");
-
(gdb) display str
-
1: str = "root:x:0:root:/root:/bin/sh:"
-
(gdb) n
-
9 printf("%s\n",token);
-
1: str = "root\000x:0:root:/root:/bin/sh:"
-
(gdb)
-
root
-
10 while ((token = strtok(NULL,":")) != NULL)
-
1: str = "root\000x:0:root:/root:/bin/sh:"
-
(gdb)
-
11 printf("%s\n",token);
-
1: str = "root\000x\000\060:root:/root:/bin/sh:"
-
(gdb)
-
x
-
10 while ((token = strtok(NULL,":")) != NULL)
-
1: str = "root\000x\000\060:root:/root:/bin/sh:"
-
(gdb)
-
11 printf("%s\n",token);
-
1: str = "root\000x\000\060\000root:/root:/bin/sh:"
-
(gdb)
阅读(1204) | 评论(0) | 转发(0) |