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

fs function help

Started by cmurtheepic, 25 November 2012 - 07:07 AM
cmurtheepic #1
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!
Kingdaro #2
Posted 25 November 2012 - 08:10 AM
fs isn't a function, it's a collection of functions used to read and write files, among other things. Some examples:


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

Hopefully this made sense.
cmurtheepic #3
Posted 25 November 2012 - 10:06 AM
ok but how would i write a filepath and is their a way i could makeit create a folder to put things i tell it to write in?
and what does if file then mean?

and here is something i wrote to ask you a few questions about what i don't get. and what i want to know how todo.


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?
 
Kingdaro #4
Posted 25 November 2012 - 11:53 AM
a. The filepath is the name of your file, and fs.open() creates it when you open it.

b. To create a folder, you would do fs.makeDir("your folder name"). If the name of your folder was "afolder", then, to write files inside that folder, you would fs.open("afolder/your file name")

c. "if file then" is a check to make sure the script doesn't crash, that's all you need to know for now.

d. If you want to write multiple lines, you use file.writeLine().

local filepath = "data"

local file = fs.open(filepath, 'w')
if file then
  file.writeLine('a line')
  file.writeLine('another line')

  file.close()
end

e. Let's just say that, for these purposes, you're storing a username and password in a file called "data". Both the username and password will be "admin." So the file "data" has in it:

admin
admin

This is what you would need to do to check the username and password individually. This is what is in "startup":

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

The file "startup" is run when the computer turns on, so when we reboot, the rest of the computer is denied to the user if they put in the wrong username and password, and we start over and ask again.

f. Registration is simple when you think about it, and you don't need to use rednet. The example script below can be named anything, but you should probably name it "register". It will ask the user for a new username and password, and then write the username and password to a file that is named their username. It also checks to see if the account already exists.


-- 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
cmurtheepic #5
Posted 27 November 2012 - 10:36 AM
thanks alot king daro i really needed the help. but really THANKS :)/> :)/> :)/> :)/> :)/>