简体   繁体   English

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

[英]Code stops working after first run

Hello stack overflow users, I have some code here:你好堆栈溢出用户,我这里有一些代码:

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

As you could guess from the title, it works the first run, but the second time it won't print out what you want, regardless of it being in the file.正如您可以从标题中猜到的,它在第一次运行时有效,但第二次它不会打印出您想要的内容,无论它是否在文件中。

Example here示例在这里

You need reset file pointer.您需要重置文件指针。 try add file:seek(0) in the begining of function尝试在函数的file:seek(0)添加file:seek(0)

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

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