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

code on reading and writing a file.

Started by subzero22, 18 August 2014 - 04:21 AM
subzero22 #1
Posted 18 August 2014 - 06:21 AM
I'm not new to lua but for some reason the fs api just gets to me.

I know it's possible to do but how would I go about making a code where the install asks the user to setup a password, Then either the password will be entered into the program itself or another file that the password program can look up and compare it to the user input.


this isn't all of the password program but it's part of it to give an example of what I'm looking for.



passlog = "password"  -- the install file would either put the password on this line or it would read it from another file.
print("Enter password")
write("> ")
input = read()
print()
while true do
  if input == passlog then
    textutils.slowPrint("passward accepted now logging in.")
    sleep(1)
    do some stuff
  else
    print("Password rejected")
    do something else
  end
end
   

I also know how to hide files so like I could have .pass file for it to read it but the user wouldn't see the file itself. But is there also a way to make it read only unless it's deleted to make a new one?
Dragon53535 #2
Posted 18 August 2014 - 07:08 AM
If you're wanting a good fs tutorial then you should check here. I refer back to it if i forget it for some reason.
subzero22 #3
Posted 18 August 2014 - 07:53 AM
I'm not writing a fs tutorial I'm aksing about fs but thanks for the info this explains a lot of it and I should be able to do what I need from it.

But I do have a question since I'm at work and can't get to my computer right now. Would this work?


write("Enter a password for your lock: ")
info = read()
local file = fs.open(directory/filename)
file.writeLine("password = "..info)
Only problem is how to I make it write a " symbole to the file?
Dragon53535 #4
Posted 18 August 2014 - 08:04 AM
No that would not be sufficient. you would need something like

write("Put stuff here")
local input = read()
local file = fs.open(directory/filename,"w") -- this opens the file in Write mode, if it's a file you can find just by typing programs into the shell, then you don't need the directory/
file.writeLine(input)
file.close() -- you must to this to close the file entirely and save it

--# That's your CREATING a password, to check you should do this

write("Input unlock")
local pass = read("*")
local file = fs.open(directory/filename,"r") -- opens the file in Read mode.
local fileData = {} -- declares fileData is an empty table. I use tables for lists like what could be in a file soooo
local line = file.readLine() -- reads the line
repeat
  table.insert(fileData,line) -- inserts the line into the table fileData at the next slot
  line = file.readLine() -- reads the next line
until line == nil -- eventually it will get to the end of the line, and the line = file.readLine() will be nil
file.close() -- closes the file again
if pass == fileData[1] then -- ASSUMING you put the password as the first line in the file it would then be put by the table.insert as the first spot which can be referenced as fileData[1]
  --do stuff
else
  --do more stuff
end

A little rundown, before i sleep and stuff. You set a variable to fs.open() which opens the file and leaves behind the variable as a handle to manipulate the file, or if you use peripherals a lot it's the same thing as doing peripheral.wrap(). There are 3 modes in fs.open() that i've used, and the correct syntax for fs.open is fs.open(string-filepath,string-mode) The modes are write, append, and read. Write (syntax is "w") clears the file entirely, and starts from the top. Append (syntax is "a") tacks on what you are putting in to the end of the program. Read (syntax is "r") allows you to read the file. When reading the file using file.readLine() if you want to read the next line, you need to specify it again by doing the same thing. It's like doing input = read(), when you look for input's value, you don't have to do another read().

There are many functions in the fs api, and a full list can be found here.

I might update this when i wake, if you have any more questions or concerns, i will attempt to answer them when i can.
Edited on 18 August 2014 - 06:14 AM
subzero22 #5
Posted 18 August 2014 - 09:43 AM
thanks I'll check back when you wake and in the mean time fiddle arround with it. The way you explained it helped some and made it understanding a little easier.

Ok I figured it out and it's working now. If I run into any other problems I'll post here. The way I got it, it doesn't use tables. I'm thinking if I could make it so each line is a new entry in a table then it could have multiple pw's for multiple people. So would that work if your version of the code works?
Edited on 18 August 2014 - 10:55 AM
KingofGamesYami #6
Posted 18 August 2014 - 01:34 PM
To add tables, simply add this to reading/writing:

--#writing
var = textutils.serialize( tbl ) --#turns a table into a writable string
--#reading
tbl = textutils.unserialize( file.readAll() ) --#turns a string into a table
Edited on 18 August 2014 - 11:34 AM