你如何阅读Lua文本文件中的特定行 [英] How do you read a specific line in a text file in Lua
本文介绍了你如何阅读Lua文本文件中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Lua中读取我选择的文本文件中的特定行,我知道如何打开它:
I have the need in Lua to read a specific line in a text file I select, I know how to open it:
filename = "hallo.txt"
fp = io.open( filename, "r" )
但我不知道如何阅读该特定文本文件中的特定行。
but I don't know how to read a specific line in that specific text file.
你怎么样?
推荐答案
如果必须多次执行,则将整个文件读入内存,将行存储在表格中。
If you have to do it several times, then read the whole file into memory, storing the lines in a table.
如果您只需要执行此操作,请尝试以下操作:
If you only have to do this once, try something like this:
local n=0
for l in io.lines(filename) do
n=n+1
if n==lineno then process(l); break end
end
这篇关于你如何阅读Lua文本文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文