-
Strange problem - strtok
Hi,
I'm using the following code:
Code:
while(!feof(f))
{
fgets (all_id, 100 , f);
sscanf (all_id,"%*s %s %s",a_id,d_id);
str[i] = a_id;
printf("\n%s",str[i]);
strtok(a_id,"G");
ida = strtok (NULL, "G");
idsa[i]=atoi(ida);
i++;
}
printf("\n%s",str[2]);
Inside the loop its printing the a_ids but outside its printing the last value of strtok(a_id,"G").
Why is this happening?
Appreciate your guidance.
Thanks,
Angkar
-
Why it's bad to use feof() to control a loop
Should the first argument of strtok be NULL? strtok(s,ch) searches s for tokens split be ch. So you are splitting NULL into tokens?
~Sven
-
Yes because in the first call I'm reaching G and in the next its end...
My string is of the form NAAG100, NBAG123...
and I want the 100,123 parts...
-
Would search the array str for the character G. Remember that 'G' is a character constant and "G" is a null terminated string. "G" is 'G' '\0'.
~Sven
-
-
Use strtok on the string that contains the numbers. Use 'G' instead of "G".
~Sven
-
Didn't get you. Can you please show an example?
-
Try just replacing the instances of "G" in your code with 'G'. Single quotes are for single character constants and double quotes are for strings. If you use double quotes it will add a terminating character at the end ( '\0' ).
~Sven