title: C put contents of file into array
body: <p>I'm trying to get tokens from a file and put it into an array. However, it keeps failing. The tokens I want are only alphanumeric and numeric. Everything else is a delimiter.</p>
  
  <p>Code:</p>
  
  <pre><code>void readFile(WORDS list, char *filepath)
  {
      int i = 0, j = 0, ch; 
      char *token, *temp;
      FILE *file = fopen(filepath, "r");
  
      if(file)
      {
          fseek(file, 0, SEEK_END);
          long f_size = ftell(file);
          fseek(file, 0, SEEK_SET);
          token = malloc(f_size);
          temp = malloc(f_size);
  
          while((ch = fgetc(file)) != EOF)
          {
              temp[i++] = (char) ch;
  
              if(regeval(temp, "^[0-9][0-9]*$"))
                  token[j++] = (char) ch;
              else if(regeval(temp, "^[a-zA-Z][a-zA-Z0-9]*$"))
                  token[j++] = (char) ch;
              else
              {
                  token[j] = '\0';
  
                  memset(token, 0, sizeof(f_size));
                  memset(temp, 0, sizeof(f_size));
                  i = j = 0;
              }
          }
      }
  
      fclose(file);
  }
  
  int regeval(char *str, char *exp)
  {
      // Make regex object
      regex_t regex;
  
      // Compile and execute expression
      int reg = regcomp(&amp;regex, exp, REG_EXTENDED);
      reg = regexec(&amp;regex, str, 0, NULL, 0);
  
      // Expression matches token
      if(!reg)
          return 1;
  
      return 0;
  }
  </code></pre>
  
  <p>If I have a file named test.txt with the content: <code>Abacus</code> in it, it will only print out <code>Abac</code>. What am I doing wrong? I thought it was a nullbyte issue so I added that line in, but nothing changed.</p>
  
  <p><strong>UPDATE</strong>: Updated code. However, it prints out Segmentation fault.</p>