|

楼主 |
发表于 2006-11-24 18:48:58
|
显示全部楼层
这是我写的测试程序,其中还有问题--没有检测value的有效性
- #include <stdio.h>
- #include <string.h>
- struct item
- {
- char name[32];
- char *value;
- struct item *next;
- };
- int main()
- {
- char *channel,*start,*end;
- struct item test;
- snprintf(test.name,31,"%s", "channel");
- test.value = (char *)malloc(128);
- snprintf(test.value,127,"%s","1-15,17-31,43,55");
-
- if(!strcasecmp(test.name,"channel"))
- {
- while(channel = strsep(&test.value,","))
- {
- start = strsep(&channel,"-");
- if(channel)
- {
- end = channel;
- printf("include channel start from %s to %s\n",start,end);
- }
- else
- printf("include channel %s\n",start);
- }
- }
- return 0;
- }
- [root@localhost program]# ./strsep
- include channel start from 1 to 15
- include channel start from 17 to 31
- include channel 43
- include channel 55
- #include <stdio.h>
- #include <string.h>
- struct item
- {
- char name[32];
- char value[128];
- struct item *next;
- };
- int main()
- {
- char *channel,*start,*end;
- struct item test;
- snprintf(test.name,31,"%s", "channel");
- //test.value = (char *)malloc(128);
- snprintf(test.value,127,"%s","1-15,17-31,43,55");
-
- if(!strcasecmp(test.name,"channel"))
- {
- while(channel = strsep(&test.value,","))
- {
- start = strsep(&channel,"-");
- if(channel)
- {
- end = channel;
- printf("include channel start from %s to %s\n",start,end);
- }
- else
- printf("include channel %s\n",start);
- }
- }
- return 0;
- }
- [root@localhost program]# ./strsep
- Segmentation fault
复制代码 |
|