最近有空空闲 整理点东西吧
1.单个文件的转换
function filetofile(infile, outfile)
local readfile = io.open(infile,"r") --读取文件
assert(readfile) --打开时验证是否出错
local writefile = io.open(outfile,"w") --写入文件(w覆盖)
assert(writefile) --打开时验证是否出错
local i=1
for rline in readfile:lines() do --一行一行
local tmp=string.gsub(rline, "^%s*(.-)%s*$", "%1") --去除首尾空格
if 0~=string.len(tmp) then --不是空行
if 1~=select(1, string.find(tmp, "#")) then --以#号开头的为注释
writefile:write(i, tmp.."\n") --写入内容
i=i+1
end
end
end
readfile:close()
writefile:close()
end
-------------------------------------------------
filetofile("in.txt", "out.txt")

本文介绍了如何在Lua中进行文件转换,包括单个文件和多个文件的批量转换。内容涉及使用lfs库来实现文件读取和操作,是lua学习者进行文件操作的实用教程。
8882

被折叠的 条评论
为什么被折叠?



