Jump to content




Color-based lock system


  • You cannot reply to this topic
3 replies to this topic

#1 Inumel

  • Members
  • 120 posts

Posted 14 April 2015 - 01: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.

#2 valithor

  • Members
  • 1,053 posts

Posted 14 April 2015 - 01:37 AM

View PostInumel, on 14 April 2015 - 01:32 AM, said:

-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 by valithor, 14 April 2015 - 01:38 AM.


#3 Inumel

  • Members
  • 120 posts

Posted 14 April 2015 - 01: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!

#4 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 14 April 2015 - 01: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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users