63 posts
Posted 25 August 2013 - 01:00 AM
Alright, so I need to have a .txt file, that stores a users password
What is an efficient way to get a password from this file, and then use it as a variable in my code to do things like print it to the screen, and check it to see if it matches with what the user entered.
8543 posts
Posted 25 August 2013 - 01:10 AM
Split into new topic.
997 posts
Location
Wellington, New Zealand
Posted 25 August 2013 - 01:51 AM
file = fs.open("filename", "r")
password = file.readAll()
file.close()
63 posts
Posted 26 August 2013 - 01:29 AM
So, I would write my password to the file… how?
331 posts
Posted 26 August 2013 - 01:35 AM
well you can read i file like the above person stated or write to it like
f = fs.open("file", "w") -- 'w' for write mode
f.write("your password here")
f.close -- close the file
i would recommending checking out the fs API
http://www.computerc...fo/wiki/Fs_(API)
47 posts
Posted 26 August 2013 - 02:16 PM
Look up the fs API on computercraft wiki and also make sure you click on and look at the fs.open() API
to read:
file = fs.open("your/file/name", "r") --The second argument "r" sets the fs API to read mode
sData = file.readAll() --this reads everything in the file and stores it in a string
file.close() --closing a file is very important, never forget this step or you will regret it ;)/>
or your could also do:
file = fs.open("your/file/name", "r") -- again opening in read mode
tData = {} --this will store the file's content
sRead = file.readLine()
-- now read from the file
while sRead do
table.insert(tData, sRead)
sRead = file.readLine()
end
--When you reach the end of the file it will return nil into sRead, since it says while sRead the while loop is checking if sRead is not false or if sRead is not nil so when it does equal nil, the loop will end
file.close() --again do not forget this
to write:
tWrite = {"this is where the data you want to write goes"}
file = fs.open("your/file/name", "w") -- the second argument "w" sets the fs API to write mode
--write to the file
for i, v in ipairs(tWrite) do
file.writeLine(v)
end
file.close() --again very important, your file will end up blank if you forget this step