Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local serialization = {}
- -- Function to serialize a table and write it to a file
- function serialization.saveTableToFile(tbl, fileName)
- if type(tbl) ~= "table" then
- error("The first argument must be a table", 2)
- end
- local file = fs.open(fileName, "w")
- if file then
- local serializedData = textutils.serialize(tbl)
- file.write(serializedData)
- file.close()
- return true
- else
- return false, "Failed to open file for writing."
- end
- end
- -- Function to read a file and deserialize its contents into a table
- function serialization.loadTableFromFile(fileName)
- if not fs.exists(fileName) then
- return false, "File does not exist."
- end
- local file = fs.open(fileName, "r")
- if file then
- local serializedData = file.readAll()
- file.close()
- local deserializedTable = textutils.unserialize(serializedData)
- if deserializedTable then
- return deserializedTable
- else
- return false, "Failed to deserialize the file content."
- end
- else
- return false, "Failed to open file for reading."
- end
- end
- return serialization
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement