Right now I have an array that gets read in from a text file and prints out like so:
1,Spam Sandwich,5
5,Video Games,3
6,Psychology,3
10,Koi Fish,99
15,Tornados,5
with a new line after each line, so the 18th member of the array is a \n in this case.
I'm interested in isolating the number after the last comma in each case but doing it in a way that the integer could have any digits, so it could read 1,Spam Sandwich,999 and isolate the 999 as well.
So far I've been able to isolate the 5 in the first line with this code:
const char delim[2] = "\n";
char *token;
token = strtok(a.array, delim);
printf("%s\n", token);
char *token2;
const char delim2[2] = ",";
token2 = token;
printf("%s\n", token2);
token2=strtok(token2, delim2);
printf("%s\n", token2);
token2 = strtok(NULL, delim2);
token2 = strtok(NULL, delim2);
printf("%s\n", token2);
which cuts the first line out of my array, delimited by the \n character, then cuts a different token, token 2, down to what's after the second comma. This seems like an inefficient way to do this if i have an indeterminate amount of lines (i want this to work for any number n lines, not just 5 like my test case)
I feel like I am on the right track with strtok(), but i'm having trouble seeing where I should proceed to.
Suggestions?