In this example, we will create a simple program to tail a file. The goal of this example is to mimic the behavior of tail -f -n0, which outputs new additions to a file. The -f argument tells the tail to follow the file and -n0 tells tail to only output to stdout new additions.
The first step is to define the mode we plan to use when opening the file we are tailing, as follows:
constexpr auto mode = std::ios::in | std::ios::ate;
In this case, we will open the file as read-only, and move the read pointer to the end of the file on open.
The next step is to create a tail function that watches for changes to a file and outputs the changes to stdout, as follows:
[[noreturn]] void
tail(std::fstream &file)
{
while (true) {
file.peek();
while(!file.eof()) {
auto pos = file.tellg();
std::string buf;
...