Reputation: 1
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...
Upvotes: 0
Views: 1746
Reputation: 2215
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
Upvotes: 0