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

Program Not Reading File Correctly?

Started by popdog15, 15 October 2013 - 04:45 PM
popdog15 #1
Posted 15 October 2013 - 06:45 PM
Here's my current code:
Spoiler

-- sets up stuff
-- put your allowed players here:
pd = peripheral.wrap("right") -- pd = playerdetector
speaker = peripheral.wrap("top")
print("yolo")

  getTable = {}
function checkFile()
  if fs.exists("authorized") == true then
	  i = 1
  loadFile = fs.open("authorized", "r")
		   
	   print("table thing")
	    -- If the last attempt to read from the file failed, stop the "while" loop.
getEvent()
   print("Banlist exists already.")
  else
   openThat = fs.open("authorized", "w")
   openThat.close()
   print("Banlist created!")
end
end
function getEvent()
    eventData = {os.pullEvent()}
    if eventData[1] == "player" then
    checkAuthority(eventData[2])
    print("Checking!")
end
end
function allowed(player)
   speaker.speak("Access granted, "..tostring(player).."Welcome in!", 1)
end

function deny(player)
   speaker.speak("Sorry, but you're not allowed in here "..player.." If you feel this was a mistake, contact Intoxicated.", 1)
end

function checkAuthority(player)
  if getTable[i] == player then
   allowed(player)
  else
    while true do
   i=i+1
   checkAuthority()
    if i <= 200 then
    deny()
    else
	 checkAuthority(player)
    end
   break
 
end
end
end
while true do
  checkFile()
end
 
The authorized file simply has the word "test" on the first line, though it still gives me access granted. Also it says "Access granted nil, welcome in!" rather than my username. Any ideas?
Bomb Bloke #2
Posted 15 October 2013 - 07:21 PM
You define "getTable" as a table, and you open the "authorised" file, but you still never load anything from the file into the table.

When your checkAuthority function first fails it calls itself without an arguement (note that having a function call itself is, in itself, generally bad practise). Because there's nothing in the table, and that second call doesn't give it a name to check against, it then compares nothing to nothing and finds them to be the same: "nil" hence gets welcomed to the system.

A somewhat improved table loader compared to the one I gave you earlier:

local getTable = {}

.
.
.

local function loadTableFromFile()
  local temp
  local loadFile = fs.open("authorized", "r")
  while true do
    temp = loadFile.readLine()
    if temp ~= nil then getTable[temp] = true else break end
  end
  loadFile.close()
end

This creates one key in the table for each player in the file, named the same as those players.

Later, when you want to check if a given name exists in the table, you can simply perform:

if getTable[player] then
  print("You're authorised!")
else
  print("I don't know you.")
end

I recommend reviewing some documentation about Lua tables. This should be a decent start.
popdog15 #3
Posted 15 October 2013 - 07:30 PM
You define "getTable" as a table, and you open the "authorised" file, but you still never load anything from the file into the table.

When your checkAuthority function first fails it calls itself without an arguement (note that having a function call itself is, in itself, generally bad practise). Because there's nothing in the table, and that second call doesn't give it a name to check against, it then compares nothing to nothing and finds them to be the same: "nil" hence gets welcomed to the system.

A somewhat improved table loader compared to the one I gave you earlier:

local getTable = {}

.
.
.

local function loadTableFromFile()
  local temp
  local loadFile = fs.open("authorized", "r")
  while true do
	temp = loadFile.readLine()
	if temp ~= nil then getTable[temp] = true else break end
  end
  loadFile.close()
end

This creates one key in the table for each player in the file, named the same as those players.

Later, when you want to check if a given name exists in the table, you can simply perform:

if getTable[player] then
  print("You're authorised!")
else
  print("I don't know you.")
end

I recommend reviewing some documentation about Lua tables. This should be a decent start.
Thank you. I spent an unnecessary amount of time trying to get this working, when I totally forgot about the previous table loader you put up. Thanks, again.