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

[lua]Read a file to a variable

Started by petterroea, 30 April 2012 - 07:18 PM
petterroea #1
Posted 30 April 2012 - 09:18 PM
I am making some software for a lockable door for my server, and i want it configurable. So i want the user to be able to put the password in a file called "pass", and then read the contents to a variable and compare to input from the person who wants to log in. How do i do that?
Luanub #2
Posted 30 April 2012 - 09:48 PM
Like this


local file = io.read("filename", "r") -- open file in read only
contents = file:read()
file:close()

while true do
write "Enter Password: "
local input = read("*")
if input == contents then
  print "correct password"
  doStuff()
  break
else
  print "invalid entry"
end
end
petterroea #3
Posted 01 May 2012 - 03:48 PM
<name> Expected at file:read()
Lyqyd #4
Posted 02 May 2012 - 03:30 AM
Try changing the io.read call to an io.open call. And make sure the file isn't blank.
Luanub #5
Posted 02 May 2012 - 03:32 AM
Try changing the io.read call to an io.open call. And make sure the file isn't blank.

Ya my bad, it should be:
local file = io.open("filename", "r")