-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-05_last_n_lines_file.lua
More file actions
35 lines (34 loc) · 1.02 KB
/
07-05_last_n_lines_file.lua
File metadata and controls
35 lines (34 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function readLastLine(filename, linesToRead)
local f = assert(io.open(filename,"r"))
local totalSize = f:seek("end")
local lineCount = 1
for i = 1, totalSize do
f:seek("end", -i)
local l = f:read()
if l == nil then
break
end
local currChar = string.sub(l, 1, 1)
if string.byte(currChar) == nil then
local j = i - 1;
f:seek("end", -j)
local line = f:read()
print(line)
f:seek("end", -i)
if lineCount < linesToRead then
lineCount = lineCount + 1
else
break
end
end
-- print("inx: "..-i)
-- print("buffer: "..l)
-- local currChar = string.sub(l, 1, 1)
-- print(string.format("Code: %d - Curr char: %s", string.byte(currChar) or -1, currChar))
end
if lineCount == 1 and linesToRead > 0 then
f:seek("set")
print(f:read("a"))
end
end
readLastLine(arg[1], tonumber(arg[2]))