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

Reading Files to check for word.

Started by Waterspark63, 18 June 2018 - 12:30 PM
Waterspark63 #1
Posted 18 June 2018 - 02:30 PM
Hey, a bit of a stupid question, so I'm trying to make a 'user' creator that when you input a name, it checks a file that has a full list of all the currently created user names, to prevent duplicates, and if it doesn't then it adds it to the file later when creating the account, but I can't figure out how to search the file for the string, any help?
Bomb Bloke #2
Posted 19 June 2018 - 03:45 AM
The easy way is to put your usernames in a table:

local usernames = {["Waterspark63"] = true, ["Bomb Bloke"] = true}

print("Enter a username:")

local user = read()

if usernames[user] then
  -- Username exists
else
  -- Username does not exist
end

You can then save / load this table using textutils.serialise() / textutils.unserialise():

-- Save table:
local outFile = fs.open("users.txt", "w")
outFile.writeLine( textutils.serialise( usernames ) )
outFile.close()

-- Load table:
local inFile = fs.open("users.txt", "r")
local usernames = textutils.unserialise( inFile.readAll() )
inFile.close()
Edited on 19 June 2018 - 01:46 AM