感谢网友发现问题,现已修改好,提交了完整程序
root@datanode2 ~]# cat s1.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *p;
char str[] = "asd@123i@456";
char *mid = (char *)malloc(30);
strcpy(mid, str);
p = strsep(&mid,"@");
printf("p = %s\n",p);
printf("str = %s\n",str);
p = strsep(&mid,"@");
printf("p = %s\n",p);
printf("str = %s\n",str);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *p;
char str[] = "asd@123i@456";
char *mid = (char *)malloc(30);
strcpy(mid, str);
p = strsep(&mid,"@");
printf("p = %s\n",p);
printf("str = %s\n",str);
p = strsep(&mid,"@");
printf("p = %s\n",p);
printf("str = %s\n",str);
return 0;
}
[root@datanode2 ~]# gcc s1.cpp -o s1
[root@datanode2 ~]# ./s1
p = asd
str = asd@123i@456
p = 123i
str = asd@123i@456
输出结果是:123
asd
strsep 是将字符串截断.返回的是截断前的字符串.剩下的是截断后的字符串.