title: Using strtok() to store numeric values in a 2D array
body: <p>I'm trying to write a program that takes a list of numbers from a file and stores each row of numbers in a 2D array using strtok(). The problem I'm having is that when I try to store values in the rows of my 2D array, the while loop goes into an infinite loop, but when i use the line token = strtok(NULL, " ") to stop the loop, I just store the first value of each line into the first cell of each column. </p>
  
  <p>What am I doing wrong? Heres is my code</p>
  
  <pre><code>int main() {
    int myArray[240][30];
    char line[240];
    char *token;
    int i, n, j;
    FILE *fp;
    fp = fopen("array_list.txt", "rt");
    j = 0;
    i = 0;
    while (fgets(line, sizeof(line), fp)) {
      token = strtok(line, " ");
  
      while (token != NULL) {
        n = atoi(token);
        myArray[i][j] = n;
        token = strtok(line, " ");
        printf("%d ", myArray[i][j]);
        token = strtok(NULL, " ");
        j++;
      }
  
      printf("\n");
      i++;
    }
    return 0;
  }
  </code></pre>
  
  <p>Here is an example of the file I'm trying to read.</p>
  
  <p>1 98 48 55 46 12 48 43 7 73 47 33<br>
                                                                                                                                                                      97 26 67 25 67 47 46 74 67 43 78 98 90 58 6 5 53 19 37 42<br>
  81 84 36 70 59 65 99 56 94 23
  79 73 27 47 98 78 8 2 3 43 84 19 59 98 51 72 2 13 55 3
  20 61 25 84 28 70 56 23 52 69
  86 70 60 90 72
  51 71 92 91 55 6 72 67 73 64 78 43 65 30 49
  99 82 40 72 5 44 20 21 22 72 99 74 27 59 25 1 26 26 22 66
  63 27 54 46 46
  91 65 77 45 94 94 95 11 77 46 64 14
  69 70 75 48 68 22 55 78 1 4
  31 76 55 12 93</p>