Hi,
I have a proble I don't understand when using strtok(). It seems that if I
make a call to strtok(), then make a call to another function that also
makes use of strtok(), the original call is somehow confused or upset.
I have the following code, which I am using to tokenise some input which is
in th form x:y:1.2:
int tokenize_input( Sale *sale, char *string){
char *temp;
int temp_int;
int result = TRUE;
if((temp = strtok(string, ":")) == NULL){
result = FALSE;
} else {
sale -sale_id = atoi(temp);
}
if((temp = strtok('\0',":" )) == NULL){
result = FALSE;
} else {
if(get_date(tem p)
line, my problem started*/
strncpy(sale -date, temp, DATE_LENGTH);
} else
{
/*These were added at the same time*/
result = FALSE;
/**/
}
/**/
}
if((temp = strtok('\0',"." )) ==
NULL){ /*this now returns NULL*/
result = FALSE;
} else {
temp_int = atoi(temp)*100;
}
if((temp = strtok('\0',":" )) == NULL){
result = FALSE;
} else {
temp_int = temp_int + atoi(temp);
sale -price = temp_int;
}
return result;
}
get_date() is also using strtok(). It all worked fine until I added the
marked lines in order to do some validation of input, at which point the
later strtok() began returning NULL.
Can anyone explain why this would occur and how can get around it?
Thanks for your help
Michael
I have a proble I don't understand when using strtok(). It seems that if I
make a call to strtok(), then make a call to another function that also
makes use of strtok(), the original call is somehow confused or upset.
I have the following code, which I am using to tokenise some input which is
in th form x:y:1.2:
int tokenize_input( Sale *sale, char *string){
char *temp;
int temp_int;
int result = TRUE;
if((temp = strtok(string, ":")) == NULL){
result = FALSE;
} else {
sale -sale_id = atoi(temp);
}
if((temp = strtok('\0',":" )) == NULL){
result = FALSE;
} else {
if(get_date(tem p)
-1){ /* when I added this
strncpy(sale -date, temp, DATE_LENGTH);
} else
{
/*These were added at the same time*/
result = FALSE;
/**/
}
/**/
}
if((temp = strtok('\0',"." )) ==
NULL){ /*this now returns NULL*/
result = FALSE;
} else {
temp_int = atoi(temp)*100;
}
if((temp = strtok('\0',":" )) == NULL){
result = FALSE;
} else {
temp_int = temp_int + atoi(temp);
sale -price = temp_int;
}
return result;
}
get_date() is also using strtok(). It all worked fine until I added the
marked lines in order to do some validation of input, at which point the
later strtok() began returning NULL.
Can anyone explain why this would occur and how can get around it?
Thanks for your help
Michael
Comment