December 2018
Intermediate to advanced
552 pages
12h 18m
English
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]] voidtail(std::fstream &file){ while ...Read now
Unlock full access