如何使用 io.read() 读取特定行
How to read specific line using io.read()
这就是我想要做的:
io.open("__Equivalent-Exchange__/config/EMCfixed.lua", "r")
var1 = io.read(,*n)
现在我希望能够设置用 io.open
指定的文件的行。找到的是这样的:
When called with a file name, it opens the named file (in text mode), and sets its handle as the default input file. When called with a file handle, it simply sets this file handle as the default input file. When called without parameters, it returns the current default input file.
这对我没有帮助,所以我找到了 io.lines
部分。:
Opens the given file name in read mode and returns an iterator function that works like file:lines(···)
over the opened file. When the iterator function detects the end of file, it returns no values (to finish the loop) and automatically closes the file.
The call io.lines()
(with no file name) is equivalent to io.input():lines("*l")
that is, it iterates over the lines of the default input file. In this case it does not close the file when the loop ends."
但是,如何指定要读取的行?
P.S。有关我发现 this page 主题的其他信息,我不明白。但它可能会帮助你在帮助我的过程中。
您不能直接跳转到指定的行,因为那样需要您知道该行在哪里。至少在此时不读取整个文件,这只有在行具有固定长度的情况下才有可能(在这种情况下您可以使用 file:seek)。但是,如果您没有固定长度的行,则必须遍历这些行,边走边数:
function getNthLine(fileName, n)
local f = io.open(fileName, "r")
local count = 1
for line in f:lines() do
if count == n then
f:close()
return line
end
count = count + 1
end
f:close()
error("Not enough lines in file!")
end
Edit:请注意,如果您正在搜索同一文件的多行(例如,您需要第 3、5 和 8 行),则不应使用此功能。在这种情况下,上面的函数会打开文件 3 次——这是对系统资源的浪费。相反,您可以定义一个要在每个数字上调用的函数,并在那里检查匹配的行号:
function checkLine(lineNumber, lineContent)
-- Disregard odd line numbers
if lineNumber % 2 == 0 then
-- do something with lineContent
end
end
local f = io.open(fileName, "r")
local count = 1
for line in f:lines() do
checkLine(count, line)
end
f:close()
这就是我想要做的:
io.open("__Equivalent-Exchange__/config/EMCfixed.lua", "r")
var1 = io.read(,*n)
现在我希望能够设置用 io.open
指定的文件的行。找到的是这样的:
When called with a file name, it opens the named file (in text mode), and sets its handle as the default input file. When called with a file handle, it simply sets this file handle as the default input file. When called without parameters, it returns the current default input file.
这对我没有帮助,所以我找到了 io.lines
部分。:
Opens the given file name in read mode and returns an iterator function that works like
file:lines(···)
over the opened file. When the iterator function detects the end of file, it returns no values (to finish the loop) and automatically closes the file.The call
io.lines()
(with no file name) is equivalent toio.input():lines("*l")
that is, it iterates over the lines of the default input file. In this case it does not close the file when the loop ends."
但是,如何指定要读取的行?
P.S。有关我发现 this page 主题的其他信息,我不明白。但它可能会帮助你在帮助我的过程中。
您不能直接跳转到指定的行,因为那样需要您知道该行在哪里。至少在此时不读取整个文件,这只有在行具有固定长度的情况下才有可能(在这种情况下您可以使用 file:seek)。但是,如果您没有固定长度的行,则必须遍历这些行,边走边数:
function getNthLine(fileName, n)
local f = io.open(fileName, "r")
local count = 1
for line in f:lines() do
if count == n then
f:close()
return line
end
count = count + 1
end
f:close()
error("Not enough lines in file!")
end
Edit:请注意,如果您正在搜索同一文件的多行(例如,您需要第 3、5 和 8 行),则不应使用此功能。在这种情况下,上面的函数会打开文件 3 次——这是对系统资源的浪费。相反,您可以定义一个要在每个数字上调用的函数,并在那里检查匹配的行号:
function checkLine(lineNumber, lineContent)
-- Disregard odd line numbers
if lineNumber % 2 == 0 then
-- do something with lineContent
end
end
local f = io.open(fileName, "r")
local count = 1
for line in f:lines() do
checkLine(count, line)
end
f:close()