|
|
请高手讲讲在sco unix 5.xx版本下, c语言strtok()和memcpy()函数的使用。 (本问题的用意:分割字符串。) 最好能有c语言编程的源程序。
| threehair 回复于:2002-11-06 10:37:40
| 函数名: strtok 功 能: 查找由在第二个串中指定的分界符分隔开的单词 用 法: char *strtok(char *str1, char *str2); #include <string.h> #include <stdio.h>
int main(void) { char input[16] = "abc,d"; char *p;
/* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, "," ; if (p) printf("%s\n", p);
/* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token */ p = strtok(NULL, "," ; if (p) printf("%s\n", p); return 0; } 函数名: memcpy 功 能: 从源source中拷贝n个字节到目标destin中 用 法: void *memcpy(void *destin, void *source, unsigned n); 程序例:
#include <stdio.h> #include <string.h> int main(void) { char src[] = "******************************"; char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709"; char *ptr; printf("destination before memcpy: %s\n", dest); ptr = memcpy(dest, src, strlen(src)); if (ptr) printf("destination after memcpy: %s\n", dest); else printf("memcpy failed\n" ; return 0; }
| | edwardcj 回复于:2002-11-14 15:58:54
| 提醒一下,调用strtok会破坏原字符串,如上,第一次调用p = strtok(input, "," ;后input的内容就变成"abc\x0d"了。
| | thinkeryy 回复于:2003-04-18 12:14:50
| 同意edwardcj的说法。因此如果两个分隔符之间没有内容时(如"abc,,de"),调用strtok(NULL,",")会有问题。
建议自己写一个。我就自己写了一个。
| | 风舞 回复于:2003-05-16 19:05:06
|
我是菜鸟,以后请大家多多关照。
strtok有两个参数,第二个参数可以用“|#,”!
| |
|