| Summary: | strsep() | ||
|---|---|---|---|
| Product: | Base System | Reporter: | zh_xf <zh_xf> |
| Component: | misc | Assignee: | freebsd-bugs (Nobody) <bugs> |
| Status: | Closed FIXED | ||
| Severity: | Affects Only Me | ||
| Priority: | Normal | ||
| Version: | Unspecified | ||
| Hardware: | Any | ||
| OS: | Any | ||
bad program. Add inputstring=(char *) malloc(1024); before the strcpy(inputstring,"..."); -- Kevin Street street@iname.com State Changed From-To: open->closed OOps. I replied to this one privately and forgot to close the PR when the originator mailed me confirmation. Pilot error. :-) |
Hi: I am a new user to FreeBSD. When I use the following program,I expected the out put would be; argv=aaa argv=bbbb argv=dd argv=kkkk but actually the output is argv= argv=bbbb argv=dd argv=kkkk I don't where argv[0] was missing. #include <string.h> #include <stdio.h> void main(){ char **ap, *argv[10],*inputstring; //[10] int i; strcpy(inputstring,"aaa\tbbbb\tdd\tkkkk"); for (ap = argv; (*ap = strsep(&inputstring, "\t")) != NULL;) if (**ap != '\0') if (++ap >= &argv[10]) break; for(i=0;i<4;i++) printf("argv=%s\n",argv[i]); } Fix: Sorry to use following method to fix the problem #include <string.h> #include <stdio.h> void main(){ char **ap, *argv[10],*inputstring; //[10] char s3[32]; int i; strcpy(inputstring,"aaa\tbbbb\tdd\tkkkk"); i=0; for (ap = argv; (*ap = strsep(&inputstring, "\t")) != NULL;) { if(i==0) strcpy(s3,*ap); i++; if (**ap != '\0') if (++ap >= &argv[10]) break; } strcpy(argv[0],s3); for(i=0;i<4;i++) printf("argv=%s\n",argv[i]); }