title: C: read only last line of a file. No loops
body: <p>Using C, is there a way to read <strong><em>only the last line</em></strong> of a file without looping it's entire content?</p>
  
  <p>Thing is that file contains <strong>millions</strong> of lines, each of them holding an integer (long long int). The file itself can be quite large, I presume even up to 1000mb. I know for sure that the last line won't be longer than 55 digits, but could be 2 only digits as well. It's out of options to use any kind of database... I've considered it already.</p>
  
  <p>Maybe its a silly question, but coming from PHP background I find it hard to answer. I looked everywhere but found nothing <em>clean</em>. </p>
  
  <p>Currently I'm using:</p>
  
  <pre><code>if ((fd = fopen(filename, "r")) != NULL) // open file
  {
      fseek(fd, 0, SEEK_SET); // make sure start from 0
      while(!feof(fd))
      {
          memset(buff, 0x00, buff_len); // clean buffer
          fscanf(fd, "%[^\n]\n", buff); // read file *prefer using fscanf
      }
      printf("Last Line :: %d\n", atoi(buff)); // for testing I'm using small integers
  }
  </code></pre>
  
  <p>This way I'm looping file's content and as soon as file gets bigger than ~500k lines things  slow down pretty bad.... </p>
  
  <p>Thank you in advance.
  maxim</p>