I was asked by an interviewer that how would I implement tail (yes, the one in linux shell). My answer was, first seek to the end of the file, then read characters one-by-one forward, if encounters a \n, means one line is down, blah blah blah. I assume my answer is correct.
Then I found this problem, which seek should I use to implement tail? I thought I can simply use seekg (C++ thing?), but I was told that I should use lseek (linux system call?).
So including fseek (ANSI C thing?), which one should I use to implement tail?
And is there any big difference between them?
Use
seekgwhen using the C++ IOstreams library.seekpis no use here, since it sets the put pointer.Use
fseekwhen using the C stdio library. Uselseekwhen using low-level POSIX file descriptor I/O.The difference between the various seek functions is just the kind of file/stream objects on which they operate. On Linux,
seekgandfseekare probably implemented in terms oflseek.