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

[Question] Help with the fs api?

Started by deity12, 23 November 2012 - 07:02 AM
deity12 #1
Posted 23 November 2012 - 08:02 AM
Ok, simply, if I'm running a server the computers reboot every time the server restarts, how can I make it so, whenever the computer performs an action it saves some sort of file. So, when the computer restarts (and automatically runs my program) i wan to stick a command to load the file's content and set variables accordingly. So, it can continue where it was with little interruption. I tend to learn best of just a piece of code, so I can study it for a while until I understand it. Thanks in advance!
Grim Reaper #2
Posted 23 November 2012 - 08:43 AM
Here is a quick solution that I wrote up for you. This list of functions allows you to save/read variables to/from a file using tables.
Keep in mind, this script will not allow you to save tables!
Saving variables would look something like this:

local variables = {1, 2, 3, 4, "Five."}
saveVariables(variables, "/save")
The contents of "save" would look like this:

1.0
2.0
3.0
4.0
Five.
Loading from "save":

local variables = loadVariables("/save")
Keep in mind that all variables read from a saved file will have a value of string. You'll have to cast variables back to what you'd like their types to be.

The code below has been edited thanks to the suggestion of Kingdaro below.


-- This function takes a table that contains all arguments that
-- you wish to save.
function saveVariables(variablesTable, savePath)
        -- Get a handle on the file we are to save to.
        local fileHandle = fs.open(savePath, 'w')
        if fileHandle then
                fileHandle.write(textutils.serialize(variablesTable))
                fileHandle.close()
        else
                error("File handle could not be obtained during saving.")
        end
end

-- This function returns a table containing all of the variables read
-- from the given file path in a linear fashion (top to bottom).
function loadVariables(loadPath)
        local variablesTable = {}
        -- Get handle on the file we are to load from.
        local fileHandle = fs.open(loadPath, 'r')
        if fileHandle then
                local fileContents = fileHandle.readAll()
                fileHandle.close()

                variablesTable = textutils.unserialize(fileContents)
                return variablesTable
        else
                error("File handle could not be otained during loading.")
        end
end
Kingdaro #3
Posted 23 November 2012 - 09:02 AM
I'd hate to throw away all of your hard work, Grim, but that's basically what textutils.serialize/unserialize does ._.
Grim Reaper #4
Posted 23 November 2012 - 09:12 AM
I'd hate to throw away all of your hard work, Grim, but that's basically what textutils.serialize/unserialize does ._.

I don't mind it. If there is a more effecient/pre-written solution, that's what Computer Science is all about. It's for the best; no problem ;)/>/>
I'll just add the additional table functionality.

EDIT:
Here's the better code, thanks Kingdaro.


-- This function takes a table that contains all arguments that
-- you wish to save.
function saveVariables(variablesTable, savePath)
        -- Get a handle on the file we are to save to.
        local fileHandle = fs.open(savePath, 'w')
        if fileHandle then
                fileHandle.write(textutils.serialize(variablesTable))
                fileHandle.close()
        else
                error("File handle could not be obtained during saving.")
        end
end

-- This function returns a table containing all of the variables read
-- from the given file path in a linear fashion (top to bottom).
function loadVariables(loadPath)
        local variablesTable = {}
        -- Get handle on the file we are to load from.
        local fileHandle = fs.open(loadPath, 'r')
        if fileHandle then
                local fileContents = fileHandle.readAll()
                fileHandle.close()

                variablesTable = textutils.unserialize(fileContents)
                return variablesTable
        else
                error("File handle could not be otained during loading.")
        end
end
deity12 #5
Posted 23 November 2012 - 04:01 PM
Thankyou very much, I will take away this code and give it a good read over, to make sure I understand it