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

Compare data from a file to inputs

Started by Alerith, 18 April 2013 - 09:28 PM
Alerith #1
Posted 18 April 2013 - 11:28 PM
Hello everybody, this is my code:

Spoiler

local Users = {}
function save(Users,save)
local file = fs.open("save", "a")
file.write(textutils.serialize(Users))
file.close()
end
function load(save)
local file = fs.open("save", "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
while true do
  term.clear()
  term.setCursorPos(1,1)
  print("1. Login")
  print("2. Register")
  print("3. Exit")
  opc = io.read()
  if opc == "1" then
load(save)
term.clear()
term.setCursorPos(1,1)
	write("User: ")
	user = io.read()
	write("Password: ")
	pass = read("*")
	if Users[user] and Users[user].password == pass then
  print("Access")
  print("Welcome back "..user)
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)
  print("1. Change Password")
  print("2. Exit")
  opc = io.read()

  if opc == "1" then
   term.clear()
   term.setCursorPos(1,1)
   repeat
	write("Password: ")
	pass = read("*")
	write("Rewrite Password: ")
	pass2 = read("*")
	if pass ~= pass2 then
	 print("Passwords don't match")
	end
	Users[user].password = pass
   until pass == pass2
  end

  if opc == "2" then
   term.clear()
   term.setCursorPos(1,1)
   print("Cerrando sesion...")
   sleep(1)
  end
else
  print("Denied")
end
	sleep(1)
  end
  if opc == "2" then
	term.clear()
term.setCursorPos(1,1)
	repeat
  repeat
   write("User: ")
   user = io.read()
   write("Password: ")
   pass = read("*")
   write("Rewrite Password: ")
   pass2 = read("*")
	if pass ~= pass2 then
	 print("Passwords don't match")
	end
  until pass == pass2
  Users[user] = {}
  Users[user].password = pass
  save(Users,save)
  print("Account Created")
  sleep(1)
	until pass == pass2
  end
if opc == "3" then
os.reboot()
  end
end

Here is the problem: when I try to load the file, calling the function, I dont know how to compare that data with the one that the user inputs. In the register part, the function saves that table, is every ok. If I dont use the files and functions, the program works perfectly, but obviously if I reboot, all saved data in the tables will be lost.

Thanks in advance
Kingdaro #2
Posted 19 April 2013 - 12:31 AM
It appears as though you load the file, but you don't assign it to the Users table.

Users = load()
It also doesn't look like the "save" variable you're using with the load function has a purpose.
Alerith #3
Posted 19 April 2013 - 12:47 AM
It appears as though you load the file, but you don't assign it to the Users table.

Users = load()
It also doesn't look like the "save" variable you're using with the load function has a purpose.

Thank you, you solved my problem. I erased the "save" from the both functions and asigned the load function to Users.

But I have other problem now.. If I enter 2 or more accounts, when I want to login with any of them, I can't access.