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

Saving tables in files?

Started by Stycoon, 20 December 2015 - 05:02 PM
Stycoon #1
Posted 20 December 2015 - 06:02 PM
After several days of fighting with this program and referencing this website (along with the api computercraft info pages) I have finally
produced a backbone application of what will be a much larger program.

Yes, I have tested this code, and it works.
Yes, there are better ways of handling the separate functions.

The nature of the program is rather simple, It asks if you want to insert a new computer into the table, it then saves that table to a file. It will recall the file and print off each computer with it's corresponding ID.

I am using minecraft 1.7.10 w/ Forge and computercraft 1.75 (craftOS 1.7)

–SAVING and LOADING functions

function saveTofile(mytable,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(mytable))
file.close()
end

function loadFromfile(name)
local file = fs.open(name, "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end


–Input the new computer!

function input()
local i = 1
local nmcompsid = {}
nmcompsid = loadFromfile("IDcompNM")
for m = 1, #nmcompsid do
i = i+1
end
while true do
term.clear()
term.setCursorPos(1,1)
write("Comp ID: ")
compid = read()
compid = tonumber(compid)
if compid == 0 then
saveTofile(nmcompsid,"IDcompNM")
break
end
write("Comp Name: ")
compnm = read()
compnm = tostring(compnm)

table.insert(nmcompsid, {compid, compnm})

term.clear()
term.setCursorPos(1,1)
print("computer just entered")
print(nmcompsid[2], " is id ", nmcompsid[1])
i=i+1
os.sleep(3.0)
end
end


–Print the entire table

function printdb()
mytable = {}

mytable = loadFromfile("IDcompNM")

for i = 1, #mytable do
write(mytable[2])
write(" is id: ")
print(mytable[1])
end
os.sleep(3.0)
end



–This is the main program

local fail = true

while fail do
term.clear()
term.setCursorPos(1,1)
write("Input new Computer Y/N? ")
YN = read()
YN = tostring(YN)
if YN == "Y" or YN == "y" or YN == "N" or YN == "n" then
fail = false
if YN == "Y" or YN == "y" then
input()
end
printdb()
end
end
Lyqyd #2
Posted 20 December 2015 - 07:42 PM
I don't know what your question is, but I split this off from the ancient topic you'd posted it in.
Lupus590 #3
Posted 20 December 2015 - 08:58 PM
You need to serialise the table in order to write it to a file: http://www.computercraft.info/wiki/Textutils.serialize
You will need to un-serialise it if you load the file: http://www.computercraft.info/wiki/Textutils.unserialize
TheOddByte #4
Posted 20 December 2015 - 09:19 PM
You need to serialise the table in order to write it to a file: http://www.computerc...utils.serialize
You will need to un-serialise it if you load the file: http://www.computerc...ils.unserialize
It's pretty hard to understand what the OP's question is, because in the code he posted the functions are indeed using serialize and unserialize