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

Color-based lock system

Started by Inumel, 14 April 2015 - 12:32 AM
Inumel #1
Posted 14 April 2015 - 02:32 AM
I'm trying to make a color-based locking system, wherein you use the paint program to mark out a code using colors, which is then read by another program and used as a key.

Applying one of each color, and reading the file outside of the paint progrem i've made the key

white:0
orange:1
magenta:2
lightBlue:3
yellow:4
lightGreen:5
pink:6
grey:7
lightGrey:8
cyan:9
purple:a
blue:b
brown:c
green:d
red:e
black:f

Which is all fine.
this is my attempt at reading a file:

print("please input a file name for testing")
ftc = tostring(read())
io.open(ftc,"r")
comp = ftc:read()
ftc:close()
print(comp)
It fails at line 4: "comp = ftc:read()": attempt to call nill

Now, maybe it simply can't read the file as it was created using paint, or maybe i'm doing something horrifically incorrect, sadly I'm unsure. So here I am, and help is appreciated.
valithor #2
Posted 14 April 2015 - 02:37 AM
-snip

I am unfamiliar with the io api, seeing as it is pure lua, and the java implementation of a file system is the fs api for CC. So saying that I am unable to say whether or not read is even a function you could use with io, but that is not your actual problem here.

Your problem is you are never setting ftc to the handle created when using io.open. io.open returns a table, which contains the functions you would use to get the information from the file.

Example:

file = io.open(ftc,"r") --# setting file equal to what io.open returns.
comp = ftc:read()
ftc:close()

Edit:
checked the api
Edited on 14 April 2015 - 12:38 AM
Inumel #3
Posted 14 April 2015 - 02:46 AM
Ah. It's been quite some time since I've messed with files, Thank's for the help and pointing out my derp!
Creator #4
Posted 14 April 2015 - 02:46 PM
hre is some code that will

1- read file
2- structure the content


local file = fs.open(path,"r")
local data = file.readAll()
file.close()
local buffer = {}
for token in data:gmatch("[^\n]+") do
buffer[#buffer + 1] = token
end
local finalTable = {}
for i,v in pairs(buffer) do
  local secondBuffer = {}
  for token in v:gmatch("[^:]+")  do-- : might be a magic symbol, if it is, preceed it by %
  secondBuffer[#secondBuffer+ 1] = token
end
finalTable[secondBuffer[1]] = secondBuffer[2]
end

tadaaaa