This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jamd315's profile picture

Error when reading an empty disk

Started by jamd315, 04 February 2014 - 05:06 PM
jamd315 #1
Posted 04 February 2014 - 06:06 PM
I have a computer with a disk drive, and when I put a disk in it reads the disk and checks for this weeks password. My problem is that when the file on disk containing the password doesn't exist (empty disk) the program gives me the attempted to index a nill value error. I've been trying to get OR to work, but I don't know how.

local file = fs.open("disk/pass", "r") or file = "noFile"
local data = fs.readAll() or check = "noPass"
otherwise, when the file exists and the password is correct, it works fine.
Bomb Bloke #2
Posted 04 February 2014 - 06:20 PM
"fs.readAll()" will give you an attempt to index nil regardless. I think you meant "file.readAll()", which'll still error out if "file" is a string.

I would use something like this:

local data
local check = "noPass"
if fs.exists("disk/pass") then            -- If the password file exists, then...
  local file = fs.open("disk/pass","r")   -- ... get a file handle...
  data = file.readLine()                  -- ... use it to read the data...
  file.close()                            -- ... then close it.
  check = nil     -- Or whatever you want "check" to be when the read was a success.
end
Edited on 04 February 2014 - 05:21 PM
jamd315 #3
Posted 04 February 2014 - 06:23 PM
Thanks, I didn't realize I was using fs.readAll()