1 year ago

#361713

test-img

Bug

Strtok returns the same word after being called using token = (NULL, "\n")

I am trying to tokenize a line read from a file but strtok() always returns the first word. Here is the code I have currently:

    fgets(line, 40, fp);
    token = strtok(line, " ");
    b[i].first_name = malloc(strlen(line));
    strcpy(b[i].first_name, line);

    token = strtok(NULL, "\n");
    b[i].last_name = malloc(strlen(line));
    strcpy(b[i].last_name, line);
    
    b[i].number_order_registered = malloc(sizeof(int));
    *(b[i].number_order_registered) = i+1;

    printf("-%s, %s...%d!\n", b[i].last_name, b[i].first_name, *b[i].number_order_registered);
    fprintf(fOut, "-%s, %s...%d!\n", b[i].last_name, b[i].first_name, *b[i].number_order_registered);

token

fgets

strtok

0 Answers

Your Answer

Accepted video resources