<p>I'm wondering how you can implement a program similar to <code>tail -f</code> in C/C++, a program that watches for and processes new lines added to a log file?</p>

[{'answer_id': 22384, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 0, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/18632/how-to-monitor-a-text-file-in-realtime#18635">See here</a></p>\n\n<p>You could either call out to tail and retrieve the stream back into your app, or as it\'s open source, maybe try to pull it into your own code.</p>\n\n<p>Also, it is possible in C++ iostream to open a file for viewing only and just read to the end, while buffering the last 10-20 lines, then output that.</p>\n'}, {'answer_id': 22399, 'author': 'jj33', 'author_id': 430, 'author_profile': 'https://Stackoverflow.com/users/430', 'pm_score': 4, 'selected': True, 'text': '<p>You can use fseek() to clear the eof condition on the stream.  Essentially, read to the end of the file, sleep for a while, fseek() (without changing your position) to clear eof, the read to end of file again.  wash, rinse, repeat.  man fseek(3) for details.</p>\n\n<p>Here\'s what it looks like in perl.  perl\'s seek() is essentially a wrapper for fseek(3), so the logic is the same:</p>\n\n<pre><code>wembley 0 /home/jj33/swap &gt;#&gt; cat p\nmy $f = shift;\nopen(I, "&lt;$f") || die "Couldn\'t open $f: $!\\n";\n\nwhile (1) {\n  seek(I, 0, 1);\n  while (defined(my $l = &lt;I&gt;)) {\n    print "Got: $l";\n  }\n  print "Hit EOF, sleeping\\n";\n  sleep(10);\n}\nwembley 0 /home/jj33/swap &gt;#&gt; cat tfile\nThis is\nsome\ntext\nin\na file\nwembley 0 /home/jj33/swap &gt;#&gt; perl p tfile\nGot: This is\nGot: some\nGot: text\nGot: in\nGot: a file\nHit EOF, sleeping\n</code></pre>\n\n<p>Then, in another session:</p>\n\n<pre><code>wembley 0 /home/jj33/swap &gt; echo "another line of text" &gt;&gt; tfile\n</code></pre>\n\n<p>And back to the original program output:</p>\n\n<pre><code>Hit EOF, sleeping\nGot: another line of text\nHit EOF, sleeping\n</code></pre>\n'}, {'answer_id': 22400, 'author': 'Mykroft', 'author_id': 2191, 'author_profile': 'https://Stackoverflow.com/users/2191', 'pm_score': 0, 'selected': False, 'text': '<p>I think what you\'re looking for is the select() call in c/c++. I found a copy of the man page here: <a href="http://www.opengroup.org/onlinepubs/007908775/xsh/select.html" rel="nofollow noreferrer">http://www.opengroup.org/onlinepubs/007908775/xsh/select.html</a>. Select takes file descriptors as arguments and tells you when one of them has changed and is ready for reading.</p>\n'}, {'answer_id': 22430, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 0, 'selected': False, 'text': '<p>The tail program is open source, so you could reference that.  I wondered the same thing and looked at the code a while back, thinking it would be pretty simple, but I was surprised at how complex it was.  There are lots of gotchas that have to be taken into account.</p>\n'}]
['https://Stackoverflow.com/questions/22379', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/486/']