EDIT: I will now explain this program. It will register/unregister files as Assemblies (APIs). Also it will list existing Assemblies and recover the most recent backup of System Assembly Cache (libs.index). I need it for my OS.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Reading a file's data and converting it to a table
Started by minebuild02, 14 April 2015 - 11:45 AMPosted 14 April 2015 - 01:45 PM
I am writing a new program for my OS. It needs to read a file, convert the data to a table and read this table to ensure that a file name is inside the file. I don't know how to do steps 1-2. Can anyone help me?
EDIT: I will now explain this program. It will register/unregister files as Assemblies (APIs). Also it will list existing Assemblies and recover the most recent backup of System Assembly Cache (libs.index). I need it for my OS.
EDIT: I will now explain this program. It will register/unregister files as Assemblies (APIs). Also it will list existing Assemblies and recover the most recent backup of System Assembly Cache (libs.index). I need it for my OS.
Edited on 16 April 2015 - 03:55 PM
Posted 14 April 2015 - 02:13 PM
For files, use the fs api. I don't know why you'd want to change the string returned into a table, given that there's the string.find function…
local file = fs.open( "FileToRead", "r" ) --#open the file in read mode
local data = file.readAll() --#data now contains the contents of "FileToRead"
file.close() --#always close file handles
if string.find( data, "ThingToFind" ) then
--#aha, it's there
else
--#nope, it's not
end
Posted 14 April 2015 - 02:34 PM
as for converting the data to a table do this:
cheers
local TableYouNeed = textutils.unserialize(data)
cheers
Posted 14 April 2015 - 02:50 PM
Will try to use!For files, use the fs api. I don't know why you'd want to change the string returned into a table, given that there's the string.find function…local file = fs.open( "FileToRead", "r" ) --#open the file in read mode local data = file.readAll() --#data now contains the contents of "FileToRead" file.close() --#always close file handles if string.find( data, "ThingToFind" ) then --#aha, it's there else --#nope, it's not end
Posted 14 April 2015 - 02:55 PM
Now I've got another problem: How do I remove a string from the file?
Posted 14 April 2015 - 02:58 PM
open the file in read mode, save content to string, remove whatever is annoying you, open file in write mode, and writethe data to it. If you want I could help you with some code.
Posted 14 April 2015 - 03:02 PM
It may be worth reading through This thread.
Posted 14 April 2015 - 06:11 PM
ThanksIt may be worth reading through This thread.
Posted 14 April 2015 - 07:14 PM
you could also do this
local afterItGotRemoved = string.replace(beforeItGotRemoved,whatToRemove," ")
Posted 14 April 2015 - 07:29 PM
Nope, no string.replace function. Perhaps you meant string.gsub?