title: How to go at the beginning of a line with fseek()
body: <p>I have a function where I check if the passed arguments match with the ones in a .dat file within an error margin.</p>
  
  <p>What I would like to do is find the best match. So, I read the whole file and keep track of the line which gives me the best match. After I am done with reading the file I would like to go back that specific line and read it as my final result.</p>
  
  <p>But, I could not do that "going back" part. </p>
  
  <p>My function is:</p>
  
  <pre><code>int isObjectMatches(FILE *rPtr, struct objectDatabaseList *db, double area, double length)
  {
      double sum;
      errno_t err;
      double const errorThreshold = 0.4;
      double minError = 100.0;
      int lineNo = 0, bestMatchNo = 0;
      bool match = false;
  
      if ((err = fopen_s(&amp;rPtr, "objects.dat", "r")) != 0)
          printf("Couldn't open the file to read.\n");
      else
      {
          rewind(rPtr);
          while (fscanf_s(rPtr, "%14s%lf%lf", db-&gt;dName, sizeof db-&gt;dName, &amp;db-&gt;dArea, &amp;db-&gt;dLength) == 3)
          {
              lineNo++;
              sum = pow((1 - (area / db-&gt;dArea)), 2) + pow((1 - (length / db-&gt;dLength)), 2);
  
              if (sum &gt; errorThreshold) // No possible match
              {
                  continue; // Keep reading
              }
              else
                  if (sum &lt; minError) // One possible match
                  {
                      minError = sum;
                      bestMatchNo = lineNo; // Take the line number
                      match = true;
                      continue; // Keep reading
                  }               
          }
  
          if (match)
          {
              fseek(rPtr, (bestMatchNo - 1)*sizeof(struct objectDatabaseList), SEEK_SET); // Find the line
              fscanf_s(rPtr, "%14s%lf%lf", db-&gt;dName, sizeof db-&gt;dName, &amp;db-&gt;dArea, &amp;db-&gt;dLength);
              fclose(rPtr);
              return 0;
          }
      }
      fclose(rPtr);
      return -1;
  }
  </code></pre>
  
  <p>And my struct is:</p>
  
  <pre><code>struct objectDatabaseList
  {
      char dName[15];
      double dArea;
      double dLength;
  };
  </code></pre>
  
  <p>Please note that the length of a line is not a fixed number as the "name" could be different.</p>