这将打开一个文件,读取最后一个字节,然后关闭该文件.
(defun read-final-byte (filename)
(with-open-file (s filename
:direction :input
:if-does-not-exist :error)
(let ((len (file-length s)))
(file-position s (1- len)) ; 0-based position.
(read-char s nil)))) ; don't error if reading the end of the file.
Run Code Online (Sandbox Code Playgroud)
如果要专门读取最后n一行,则必须读回不确定的字节数,直到获得n+1换行符.为了做到这一点,你要么必须向后执行块读取(更快但会在读取不需要的字节时结束),或者字节读取(更慢但允许精度和稍微更明显的算法).
我怀疑tail有一个合理的算法适用于此,所以它可能值得阅读指南tail的来源.
| 归档时间: |
|
| 查看次数: |
316 次 |
| 最近记录: |