3

I'm looking for a way to mimic the behavior of tail -f my_file within a C/C++ program, that is:
- read all file (or not - that part is not hard to figure out)
- wait for characters to be appended at the end of the file, then read them

I've tried going through tail source code but it's really not transparent (maybe because it has a lot of different behaviors depending on situations).

I can think of a way, using seekg and comparing end-of-file position to an old value after reopening. Is reopening mandatory in this case? I know selecting the FD will not work as intended since disk files are always selectable.

Another way would be to pipe the output of tail to the program but that would mean no interruption catching (since IO is always blocking) and be quite ugly (I don't want to use pipes but files directly).

There should be a proper way to do this, but I can't figure out which system utility/man page would describe the "canonical" implementation for this (fopen/fread or open/read...).

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
maxbc
  • 949
  • 1
  • 8
  • 18
  • 3
    [inotify](http://man7.org/linux/man-pages/man7/inotify.7.html) - you can probably find a simpler tutorial, but the man page is always a good start. – BoBTFish Mar 03 '16 at 13:13
  • You can check how it's implemented in [`tail`](https://github.com/goj/coreutils/blob/1adbf333557a55fdf62856abd3563e9bfb457962/src/tail.c) – janisz Mar 03 '16 at 13:16
  • There is no language C/C++. You mean C++? – too honest for this site Mar 03 '16 at 13:24
  • @Olaf he means C or C++... – Pandrei Mar 03 '16 at 13:26
  • @Pandrei: Sorry, I'm no clairvoyant like you. – too honest for this site Mar 03 '16 at 13:27
  • @Olaf I always write C code with system calls within C++ programs. If there is a C++ way of doing, I do. But in Unix environment, C++ way of doing is usually by using C system headers. If there is a "pure" C++ way of doing I take it. – maxbc Mar 03 '16 at 13:32
  • @duffymo Noted and edited out =) – maxbc Mar 03 '16 at 13:34
  • 1
    @maxbc: As the headers then use special C++ constructs (`extern "C"`) this is not C any more. And the language tghe libraries are written does not even matter, because they are in binary format and just follow a specific ABI (which the headers signal to use to the **C++** compiler). So, no, it is not C. Whoever told you C is C++ does not know at least one of them well enough to teach others. Do some research on your own! And even identical syntax does not imply identical semantics. – too honest for this site Mar 03 '16 at 13:37
  • Usually you should select the language tag according to what compiler you intent to compile the code with, unless the question requires expertise in multiple languages. – Galik Mar 03 '16 at 13:40
  • @Olaf I'm not saying it's the same language. But all libc and system headers are unofficially intended (that's my interpretation) to work natively with C++. Not all C code. But definitely glibc and unix headers (with maybe the exception of the weird `execv` prototype). Is it very wrong to never use `extern "C"` for those cases ? – maxbc Mar 03 '16 at 15:06
  • @Olaf Also, to justify saying both languages, yes I'm planning to use a C++ compiler but I'm looking for a different solution than the one marked duplicate (which is based on libstdc++, whereas inotify is a C library, which is the answer I was looking for, even though I will use it in C++ code). – maxbc Mar 03 '16 at 15:14

0 Answers0