strtok function
807578Jan 7 2003 — edited Jan 8 2003Hi,
I downloaded Sun One 7 Enterprise on to my Sun Solaris 5.8 box. I am porting a code from HP UX on to SUN Solaris. On SUN platform, I am having problems with strtok. strtok does not stop at the next delimiter if there is nothing in between the delimiters (ex: x,,,y,z with "," being the delimiter - the first strtok command gets x, the 2nd strtok call gives y instead of NULL or '' and so on). This worked on HP UX. Please help. The sample code is as foll:
#include <stdio.h>
#include <string.h>
main(int argc, char **argv) {
char assetname[10], fid[10], ftype[10], flen[10],
char schema[10], def[10], line[100];
strcpy(line,"Login,8,4,20,PRTL:Peon,,");
strcpy(assetname,strtok(line,","));
strcpy(fid,strtok(NULL,","));
strcpy(ftype,strtok(NULL,","));
strcpy(flen,strtok(NULL,","));
strcpy(schema,strtok(NULL,","));
strcpy(def,strtok(NULL,","));
.....
.....
}
As seen in the above code, the variable 'line' is parsed using strtok. The code fails at the last strok, strcpy(def,strok(NULL,",")) because there was no value between the last 2 commas in the variable line. Please help
Thanks
Atul