I'm trying to break apart a string in C using strtok(), and assign the values I get to a property of a struct within an array.
Here is what the string (expected input) looks like:
steve 31516.john 31516.
And my code (ignoring variable definitions):
while(fgets(line, sizeof(line), f) != NULL);
char* tok = strtok(line, ".");
while(tok != NULL){
char* buf = strdup(tok);
calendar[i].user = strtok(tok, " ");
char* date = strtok(NULL, " ");
calendar[i].date = atoi(date);
tok = strtok(NULL, ".");
free(buf);
i++;
}
The expected output I'd like to have is something like this:
calendar[0].user = "steve"
calendar[0].date = 31516
calendar[1].user = "john"
calendar[1].date = 31516
What I'm getting, however, is this:
calendar[0].user = "steve"
calendar[0].date = 31516
The problem I'm having, is that it seems that this loop only happens once, no matter how long the original input string is that strtok() is first running on. It is assigning the variables correctly within the loop, but it is only running once.