title: Parsing Comma Separated File in C
body: <p>I'm trying to parse a file in C that is a comprised of sets of numbers (one per line). Each set is separated by a comma. For some reason I'm getting an incorrect output.</p>
  
  <p>Example input: 1,2,4,8,55,777
  Output: A bunch of what seem to be memory addresses. And when I print off the strings it's not reading 55 or 777 (or any number over two digits).</p>
  
  <pre><code>int * parseFile(char *input, int *set, int line)
  {
      char buf[BUF_SIZE];
      char *token = (char *) malloc(10 * sizeof(char));
      int i;
  
      FILE *f = fopen(input, "r");
  
      for (i = 1; i &lt; line; i++)
          fgets(buf, BUF_SIZE, f);
  
      memset(buf, 0, BUF_SIZE); // Clear the buffer.
      i = 0;
  
      if (fgets(buf, BUF_SIZE, f) != NULL) {
          token = strtok(buf, ",");
          set[i] = atoi(token);
          i++;
  
         while (set[i] != 0) {
              printf("%d\n", set[i]);
              set[i] = atoi(token);
              i++;
  
              token = strtok(NULL, ",");
          }
      }
  
      fclose(f);
  
      return set;
  }
  </code></pre>