title: Tokenizing an input file in c
body: <p>I am trying to implement a simple program to seperate each word in a file. This is my first time using strtok(), so I am trying to create something simple to see how it works. However, I cannot get the code to work properly.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  
  char* tokenize(char *tempPhrase);
  
  int main (void)
  {
  
      FILE *in;
      char file_name[] = "test1.txt";
      char buffer[100];
      char *tokens = malloc (100 * sizeof (char));
      int i;
  
      chdir("./DataFiles");
      if (( in = fopen(file_name, "r")) == NULL)
      {
              printf("Can't open %s for reading.\n", file_name);
              return 0;
      }
  
      fgets(buffer, 100, in);
      printf("%s\n", buffer);
      *tokens = tokenize (buffer);
  
      for (i = 0; tokens[i] != NULL; i++)
      {
              printf("%s\n", tokens[i]);
      }
  
      return 0;
   }
  
  
   char*  tokenize(char *tempPhrase)
  
   {
  
      char *search = " ";
      char *tempArray = malloc (20000 * sizeof (char));
  
      int i = 0;
      tempArray[i] = strtok(tempPhrase, search);
  
      while(tempArray[i] != NULL)
      {
              i++;
              tempArray[i] = strtok(tempPhrase, search);
      }
  
  
  return tempArray;
  }
  </code></pre>
  
  <p>I am getting the following errors/warnings when I try to compile:</p>
  
  <pre><code>fopen.c: In function ‘main’:
  fopen.c:28: warning: comparison between pointer and integer
  fopen.c:30: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
  fopen.c: In function ‘tokenize’:
  fopen.c:45: warning: assignment makes integer from pointer without a cast
  fopen.c:47: warning: comparison between pointer and integer
  fopen.c:50: warning: assignment makes integer from pointer without a cast
  </code></pre>
  
  <p>Why would the strtok() function make tempArray an integer? This is causing all sorts of problems throughout the program and I cannot figure it out. </p>