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

Reading a file's data and converting it to a table

Started by minebuild02, 14 April 2015 - 11:45 AM
minebuild02 #1
Posted 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.
Edited on 16 April 2015 - 03:55 PM
KingofGamesYami #2
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
Creator #3
Posted 14 April 2015 - 02:34 PM
as for converting the data to a table do this:


local TableYouNeed = textutils.unserialize(data)

cheers
minebuild02 #4
Posted 14 April 2015 - 02:50 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
Will try to use!
minebuild02 #5
Posted 14 April 2015 - 02:55 PM
Now I've got another problem: How do I remove a string from the file?
Creator #6
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.
KingofGamesYami #7
Posted 14 April 2015 - 03:02 PM
It may be worth reading through This thread.
minebuild02 #8
Posted 14 April 2015 - 06:11 PM
It may be worth reading through This thread.
Thanks
H4X0RZ #9
Posted 14 April 2015 - 07:14 PM
you could also do this

local afterItGotRemoved = string.replace(beforeItGotRemoved,whatToRemove," ")
KingofGamesYami #10
Posted 14 April 2015 - 07:29 PM
Nope, no string.replace function. Perhaps you meant string.gsub?