title: Custom implementation of "tail -f" functionality in C
body: <p><strong>EDIT:</strong> I used, finally, inotify. As stefanB says, inotify is the thing to use. I found a tail clone that uses inotify to implement the -f mode, <a href="http://distanz.ch/inotail/" rel="nofollow noreferrer">inotail</a>.</p>
  
  <p>Original question text:</p>
  
  <p>I'm trying to implement the "tail -f" logic in a C project, for prototyping purposes I developed it in python as follow:</p>
  
  <pre><code>
      # A forever loop, each 5 seconds writes a line into file.txt
      from time import *
  
      while 1:
          sleep(5)
          file = open("file.txt", "a")
          file.write("This is a test\n")
          file.close()
  </code></pre>
  
  <p>The next code follows the eof of file.txt (updated by the code above)</p>
  
  <pre><code>
      # tail -f 
      from time import *
  
      file = open("file.txt", "r")
      file.seek(0, 2)
  
      while 1:
        line = file.readline()
        if not line:
            sleep(1)
        else:
            print line
  
      file.close()
  </code></pre>
  
  <p>All works fine but the C implementation is not working (there are no check-error code). The inclusion of stdio.h, string.h and unistd.h is omitted (the colorization hides the header inclusion code).</p>
  
  <pre><code>
      #define LINE_LEN 256
  
      int main(int argc, char **argv)
      {
          FILE *f;
          char line[LINE_LEN];
  
          f = fopen("file.txt", "r");
  
          fseek(f, 0, SEEK_END);
  
          while (1)
          {
              fgets(line, LINE_LEN, f);
  
              if (strlen(line) == 0)
              {
                  sleep(1);
              }
              else
              {
                  printf("Readed: %s", line);
              } 
          }
  
          fclose(f);
  
          return 0;
      }
  </code></pre>
  
  <p>Some idea?</p>
  
  <p>Is a good idea implement it with a poll() instead the presented solution?.</p>
  
  <p>Thanks in advance.</p>