我直接给你个读出每一行的方法吧
local path = "d:\\1.txt"
local file = io.open(path,"r")
if file then
for line in file:lines() do
print(line) -- 这里就是读出每一行txt里的文字并且print到标准输出上 如果需要修改 只要改掉print就好
end
end
追问谢谢谢你了,那这样能实现自动读行了,比如有2行,他能依次读取并输入吗?还有我是想软件第二次运行再读取第二行,不是说一次性把文本全部依次读取完和输入
追答local filePath = "D:\\1.txt"
function readFile(file)
assert(file,"file open failed")
local fileTab = {}
local line = file:read()
while line do
print("get line",line)
table.insert(fileTab,line)
line = file:read()
end
return fileTab
end
function writeFile(file,fileTab)
assert(file,"file open failed")
for i,line in ipairs(fileTab) do
print("write ",line)
file:write(line)
file:write("\n")
end
end
function main()
print("start")
local fileRead = io.open(filePath)
if fileRead then
local tab = readFile(fileRead)
fileRead:close()
table.remove(tab,1)
local fileWrite = io.open(filePath,"w")
if fileWrite then
writeFile(fileWrite,tab)
fileWrite:close()
end
end
end
main()
追问谢谢了
本回答被提问者采纳