tail -f like functionality in standard C++
For those who don’t know tail -f monitors the new lines as they are added to a file in UNIX like systems. It’s a useful way of monitoring a logfile. I wanted to do something similar in C++, however it looks like you need platform specific language extensions to do this is a reasonable way (inotify on Linux). So I created the following quick and dirty hack. This hack maybe slow if you have large files, with few linefeeds, but it suits my purposes.
#include
#include
#include
using namespace std;
int find_last_linefeed(ifstream &infile) {
infile.seekg(0,ios::end);
int filesize = infile.tellg();
for(int n=1;n
infile.seekg(position,ios::beg);
string in;
infile >> in;
cout << in << endl;
}
last_position=position;
sleep(1);
}
}
[/sourcecode]