lua脚本怎么调整文件指针,不可能每次都从文件开头读吧,
1个回答
展开全部
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()的返回值写入文件保存就行了……
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询