代码在第一次运行后停止工作

大家好,我有一些代码想请教:

local input = nil
print("你想访问哪个文件?")
input = io.read();
local file = io.open(input, "r")

function infiniteLoop()
  print("你想了解哪个内容?")
  input = io.read();
  while true do
    line = file:read()
    if line == nil then break end
        if string.find(line, input) then
        print(line)
     end
  end
end

repeat
  infiniteLoop()
until false

正如你从标题中猜到的那样,它第一次运行时是可以工作的,但第二次运行时不管文件中是否存在想要的内容,都不会输出。

例子在这里

点赞
用户2328287
用户2328287

你需要重置文件指针。在函数开头尝试添加 file:seek(0)

2017-02-08 05:58:36