A. Phil
A. Phil

Reputation: 13

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

Upvotes: 1

Views: 58

Answers (1)

moteus
moteus

Reputation: 2235

You need reset file pointer. try add file:seek(0) in the begining of function

Upvotes: 1

Related Questions