简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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