繁体   English   中英

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

[英]Code stops working after first run

你好堆栈溢出用户,我这里有一些代码:

local input = nil
print("What file do you want to access?")
input = io.read();
local file = io.open(input, "r")

function infiniteLoop()
  print("What do you want to know more about?")
  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

正如您可以从标题中猜到的,它在第一次运行时有效,但第二次它不会打印出您想要的内容,无论它是否在文件中。

示例在这里

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM