title: C reading string content from file and compare equality
body: <p>I am reading content from a file and I am expecting to look for a particular value, say "london". for this I am reading the content, tokenize it by "\n", then compare each value against "london" using "strcmp". </p>
  
  <p>But I think I still do not understand how C stores data and does comparison and therefore my code below does not work as i expect. I guess I am missing some fundamental knowledge of C here, please help:</p>
  
  <p>TEST COMMAND LINE:</p>
  
  <pre><code>./myprogram datafile.txt "london"
  </code></pre>
  
  <p>INPUT datafile.txt:</p>
  
  <pre><code>london
  manchester
  britain
  ...
  </code></pre>
  
  <p>CODE myprogram.c:</p>
  
  <pre><code>int main(int argc, char **argv) {
    FILE* fl;
    char st1[2000];
    char * buffer = 0;
    long length;
  
    fl = fopen (argv[1], "r"); //the data file content is shown above
    if (fl){
      fseek (fl, 0, SEEK_END);
      length = ftell (fl);
      fseek (fl, 0, SEEK_SET);
      buffer = malloc (length+1);
      if (buffer){
          //fread (buffer, 1, length, fl);
          fread(buffer, sizeof(char), length, fl);
          buffer[length] = '\0';
      }
    fclose (fl);    
    }else{
      printf("data file not found");
      return -1;
    }  
  
  
    //firstly let's compare the value passed by command line with "london"
    strcpy(st1, argv[2]); 
    if(strcmp(st1,"london")==0)
      printf("equals\n");  //as expected, I get "equals" printed
    else
      printf("unequal\n");
  
  
    //now let's compare the values extracted from the data file, 
    char* entity = strtok(buffer, "\n"); 
    while (entity != NULL) {
      strcpy(st1, entity); //copy the value from the char pointer entity to the char array st1 so we can compare with other strings
  
      printf("%s\n", st1);  //this prints london, ....
  
      if(strcmp(st1,"london")==0)
          printf("equals\n");  //I was expecting this..
      else
          printf("unequal\n"); //but i got this...
      entity = strtok(NULL, "\n");
    }
    return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>I am expecting output from the above program to be:</p>
  
  <pre><code>equals
  london
  equals
  manchester
  unequal
  britain
  unequal
  ...
  </code></pre>
  
  <p>but I dont understand why I am getting</p>
  
  <pre><code>equals
  london
  unequal  &lt;=============== why and how to fix?
  manchester
  unequal
  britain
  unequal
  ...
  </code></pre>
  
  <p>And how should I change it so the value "london" read from the file equals the actual "string" of "london"?</p>
  
  <p>Many thanks</p>