Posted 25 November 2012 - 08:07 AM
could someon please explain to me how the fs function works and how to use it i looked on the wiki and it wasn't helpful at ALL!
local filepath = 'somefile'
-- writing "toast" to a file
local file = fs.open(filepath, 'w') -- the 'w' means that we are going to write to the file
if file then
file.write('toast')
file.close()
end
-- reading text from a file
local file = fs.open(filepath, 'r') -- the 'r' means that we are going to read the file
if file then
print(file.readAll()) -- this prints "toast"
file.close()
end
local filepath = "?"
local register = fs.open(?, "w")
if register then
register.write("--how do i makeit write to the file on multiple lines")
register.close()
end
local register = fs.open(?, "r")
if register then
-- i want it to see if like user input is equal to the contents of the file
--like lets say "username","password" but not to see if user input is equal to both username and password but just one at a time.
register.close()
end
--lets say i wanted to write a new registered user to a file that acted like a register
--from the info i would get from rednet?
--and how would i put the info from rednet into a variable to write to the register?
local filepath = "data"
local file = fs.open(filepath, 'w')
if file then
file.writeLine('a line')
file.writeLine('another line')
file.close()
end
admin
admin
local username, password -- this is just here so we can use it later.
local file = fs.open('data', 'r') -- open the 'data' file with 'r' (reading)
if file then
username = file.readLine() -- read the first line, then move on to the next one.
password = file.readLine() -- same as above, but there is no next line, so we are done here.
file.close()
end
print 'Username?'
local uinput = read()
print 'Password?'
local pinput = read()
-- read() stops the script and waits for the user to type something in.
-- it stores whatever the user said into a variable.
-- in this case, the first thing the user says will be store in "uinput", and the second in "pinput"
if uinput == username and pinput == password then
print 'Welcome.' -- welcome the user if he/she puts in the correct username and password
else
print 'Access denied.'
os.reboot() -- restart the computer.
end
-- get the username and password the user wants to register
print 'Username?'
local username = read()
print 'Password?'
local password = read()
-- if the account does not exist, go ahead and create it.
-- i already explained what is going on below.
if not fs.exists(username) then
local file = fs.open(username, 'w')
if file then
file.writeLine(username)
file.writeLine(password)
file.close()
print 'Account successfully created.'
end
else
-- if the account already exists, don't create it.
print 'Account already exists.'
end