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

Save settings in files?

Started by JohnnyX, 27 April 2014 - 06:05 PM
JohnnyX #1
Posted 27 April 2014 - 08:05 PM
I was wondering if it is possible to save certain settings so that you will have to input them only once. For example, I would like a computer to remember who the admin is by making it read a file that says so. Is that possible and if so, how can I do it?
MKlegoman357 #2
Posted 27 April 2014 - 08:27 PM
What you are looking for is fs API. This API allows to create, write, read files, also, delete files or folders, get a file/subfolder list of a specific directory and more.
Edited on 27 April 2014 - 06:28 PM
JohnnyX #3
Posted 27 April 2014 - 08:32 PM
Is this API included in cc or do I have to download it seperately? I guess since it's on the wiki it's on cc??
MKlegoman357 #4
Posted 27 April 2014 - 08:32 PM
Is this API included in cc or do I have to download it seperately? I guess since it's on the wiki it's on cc??

It is included together with CC.
TheOddByte #5
Posted 27 April 2014 - 10:05 PM
And with that said above I would suggest you look into putting your settings in a table and save it into a file using textutils.serialize.

local user = {}
user.name = "foo"
user.pass = "bar"

--# Writing to a file
local file = fs.open( "userfile", "w" ) -- Open the file in writing mode and use 'file' as a handle
file.write( textutils.serialize( user ) ) -- Write the serialized table into the file
file.close() -- It's very important to close the file when you're done!

--# Reading from it
user = {} -- Clearing the table
local file = fs.open( "userfile", "r" ) -- Open the file again but in reading mode
user = textutils.unserialize( file.readAll() ) -- Unserialize the table
file.close()

--# Print it to see the results
print( "Username: " .. user.name .. "\nPassword: " .. string.rep("*", #user.pass) )
JohnnyX #6
Posted 28 April 2014 - 07:05 PM
Thank you all!