Implement "tail -f" in C++

Hamming picture Hamming · Apr 23, 2010 · Viewed 15.4k times · Source

I want to create a small code in C++ with the same functionality as "tail-f": watch for new lines in a text file and show them in the standard output.

The idea is to have a thread that monitors the file

Is there an easy way to do it without opening and closing the file each time?

Answer

anon picture anon · Apr 23, 2010

Just keep reading the file. If the read fails, do nothing. There's no need to repeatedly open and close it. However, you will find it much more efficient to use operating system specific features to monitor the file, should your OS provide them.