string tok to char arrays
hi i am having some trouble with string tokenising....
Code:
char line "40,2,240";
char *result;
char age[20];
char hieght[20];
char wieght[20];
result= strtok(line,","))!=NULL)
strcpy(age,result);
printf("1st age :%s:\n",age);
result= strtok(line,","))!=NULL)
strcpy(height,result);
result= strtok(line,","))!=NULL)
strcpy(age,result);
result= strtok(line,","))!=NULL)
strcpy(wieght,result);
if(strtok(line,","))==NULL)
printf("end\n");
printf("age :%s: \n",age);
which produces
Code:
1st age is 20;
age ::;
end
so i guess what is happening is that result (char*) is pointing to the different memory locations of the portion of the string (line) and that when i get down the bottom result is pointing to a null.....
but my question is how do i avoid the referencing problem ??? especially if i want to do this thousands of times over a file ???
ie the idea is cut the line up and then print the char arrays ??
have been playing around with this for ( embarrassingly) days
thanks