title: Fscanf or Fgets? Reading a file line after line
body: <p>i have to write a program in C to read a file containing several line of text, each line contains two variables: a number (%f) and a string:</p>
  
  <pre><code>EX: file.txt
  ============
  24.0 Torino
  26.0 Milano
  27.2 Milano
  26.0 Torino
  28.0 Torino
  29.4 Milano
  </code></pre>
  
  <p>There is my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main (int argc, char *argv[])
  {
      int r, line = 0, found = 0;
      float temp, t_tot = 0;
      char loc[32];
  
  
      FILE *fp;
  
      fp = fopen(argv[1], "r");
  
      if (fp == NULL)
      {
          printf ("Error opening the file\n\n'");
          exit(EXIT_FAILURE);
      }
  
      if (argc == 3)
      {
          r = fscanf(fp, "%f %s\n", &amp;temp, loc);
  
          while (r != EOF)
          {
              line++;
  
              if (r == 2)
              {
                  if(strcmp(argv[2], loc) == 0)
                  {
                      t_tot += temp;
                      found++;
                  }
              }
              else
                  printf ("Error, line %d in wrong format!\n\n", line);
          }
  
          printf ("The average temperature in %s is: %.1f\n\n", argv[2], (t_tot/found);
      }
  
  }
  </code></pre>
  
  <p>The program needs to read all the line and find the city i wrote on <code>argv[2]</code>. Than it will tell me the average temperature on that city, notifying me if a line in the file is in a wrong format.</p>
  
  <p>The program is compiling correctly to me but it not output anything on screen... how can i solve that? Is it correct to use <code>fscanf</code> on this case or it's better <code>fgets</code>?</p>
  
  <p>I'm a student so, please, give me an "academical" way to resolve it :)</p>