file:seek()可用于获取及调整当前读取位置,示例如下:
//source.txt
this is line 1
this is line 2
this is line 3
this is line 4
this is line 5
//script.lua
local file = assert(io.open("source.txt"))
print(file:read("*line"))
print(file:read("*line")) --此时读取至第二行结束
local current = file:seek() --保存当前读取位置
print(file:read("*line"))
print(file:read("*line"))
file:seek("set", current) --重设读取位置
print(file:read("*line"))
//输出
this is line 1
this is line 2
this is line 3
this is line 4
this is line 3
如果要在多次运行中保存位置的话将seek()的返回值写入文件保存就行了……