我被面试官问我将如何实现尾(是的,一个在Linux的shell)。我的回答是,首先寻求到文件的末尾,然后读取字符一个接一个向前,如果遇到一个 \\ n ,意味着一条线是下降,等等等等等等。我想我的答案是正确的。
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.
然后我发现这个问题,这求我应该使用实施尾?我以为我可以简单地使用 seekg (C ++的事情吗?),但有人告诉我,我应该使用 lseek的(Linux的系统调用?)。
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?).
因此包括 fseek的(ANSI C的东西?),其中一个我应该用实施尾?而且是它们之间有很大的区别?
So including fseek (ANSI C thing?), which one should I use to implement tail? And is there any big difference between them?
推荐答案使用 seekg 使用C ++输入输出流库时。 seekp 是没有用在这里,因为它集认沽指针。
Use seekg when using the C++ IOstreams library. seekp is no use here, since it sets the put pointer.
使用 fseek的使用C stdio库时。使用使用低级别POSIX文件描述符时 lseek的 I / O。
Use fseek when using the C stdio library. Use lseek when using low-level POSIX file descriptor I/O.
之间的各种查找功能不同的是刚才那种文件/流对象在其经营。在Linux上, seekg 和 fseek的很可能在 lseek的。
The difference between the various seek functions is just the kind of file/stream objects on which they operate. On Linux, seekg and fseek are probably implemented in terms of lseek.
更多推荐
什么是`fseek`,`lseek`,`seekg`,之间的区别`seekp`?
发布评论