0

I'm getting this error at this point in the code... But I don't think there's something wrong...

function readAll(file)
  local f = io.open(file, "rb")
  local content = f:read("*all"); f:close()
  return content
end

I get this error attempt to index a nil value (local 'f') in this specific line:

local content = f:read("*all"); f:close()

Any help is appreciated Thank you

I just tried to execute the script, but I got this error... It was supposed to work without any errors...

I already tried to look around the internet, but nothing seems to be wrong with the code...

aynber
  • 22,380
  • 8
  • 50
  • 63

1 Answers1

0

first solution:

function readAll(file)
  local f = io.open(file, "ab+") -- create if it not exist
  local content = f:read("*all"); f:close()
  return content
end

second solution:

function readAll(file)
  local f = io.open(file, "rb+")
  if not f then return "" end -- check file
  local content = f:read("*all"); f:close()
  return content
end
Mike V.
  • 2,077
  • 8
  • 19