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

trying to use the FS api

Started by Dustmuz, 12 October 2014 - 06:39 AM
Dustmuz #1
Posted 12 October 2014 - 08:39 AM
Hello world :D/> hehe

i'm messing around the FS api..

and tbh.. i have no idea on how to get it to work..

im trying to save a variable from an IF statement, to a file for later use (if computer is rebooted, then it "knows" what the state of the IF was)

how would i get around that..

i have tried the
fs.open("filename","w")
fs.writeLine(*var name*)
fs.close()

as it was how i "thought" it was used.. but when i do that, it just prints 15, in my UI
tried putting the variables name infront of the FS aswell

*varname* = fs.open("filename","w")
and for the write and close aswell..

im asking here, since i couldnt locate a tutorial on the FS api on the forums :(/>

my code im testing on is very simple


mfrlaser = *open the FS file and read it to set the mfrlaser state*
while true do
  input = read()
  if input == "mfr" then
    if mfrlaser == true then
	  mfrlaser = false
    else
	  mfrlaser = true
    end
  end
end
*then the FS saving things comes here*
Bomb Bloke #2
Posted 12 October 2014 - 09:33 AM
The FS API deals with strings, for the most part. textutils.serialize() / textutils.unserialize() can convert tables (and their contents) to strings and back again, allowing you to dump other data types to disk.

Eg:

local myTable = {["mfrlaser"] = true}
local myFileHandle = fs.open("filename","w")
myFileHandle.write(textutils.serialize(myTable))
myFileHandle.close()

local myFileHandle = fs.open("filename","r")
local myTable = textutils.unserialize(myFileHandle.readAll())
myFileHandle.close()
InputUsername #3
Posted 12 October 2014 - 10:13 AM

im asking here, since i couldnt locate a tutorial on the FS api on the forums :(/>

Do you know about the wiki? It probably has the information you need.
Dustmuz #4
Posted 12 October 2014 - 11:31 AM

im asking here, since i couldnt locate a tutorial on the FS api on the forums :(/>

Do you know about the wiki? It probably has the information you need.

i started out with the WIKI.. but doesnt explain as good on HOW you use it, only how you use each line for itself, so that i got a bit confused about how to get it all to work..

The FS API deals with strings, for the most part. textutils.serialize() / textutils.unserialize() can convert tables (and their contents) to strings and back again, allowing you to dump other data types to disk.

Eg:

local myTable = {["mfrlaser"] = true}
local myFileHandle = fs.open("filename","w")
myFileHandle.write(textutils.serialize(myTable))
myFileHandle.close()

local myFileHandle = fs.open("filename","r")
local myTable = textutils.unserialize(myFileHandle.readAll())
myFileHandle.close()

so i would have to make the mfrlaser variable into a table, for the FS to work with it??
Bomb Bloke #5
Posted 12 October 2014 - 12:16 PM
so i would have to make the mfrlaser variable into a table, for the FS to work with it??

More precisely, you'd generally find a way to represent it as a string. Sticking it into a table and serialising/unserialising that is one way to do it; truth be told you could just serialise/unserialise the variable as it is, but if you find you want to store more than one variable to disk at a time, cramming them all in a table is generally the easiest way to do it.
KingofGamesYami #6
Posted 12 October 2014 - 05:53 PM
I think what you don't know is that fs.open returns a handle.

local file = fs.open( "filename", "mode" )
file.write( "hi" )
file.close()