Jump to content




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


  • You cannot reply to this topic
9 replies to this topic

#1 minebuild02

  • Members
  • 97 posts

Posted 14 April 2015 - 12: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 by minebuild02, 16 April 2015 - 04:55 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 14 April 2015 - 01: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


#3 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 14 April 2015 - 01:34 PM

as for converting the data to a table do this:

local TableYouNeed = textutils.unserialize(data)

cheers

#4 minebuild02

  • Members
  • 97 posts

Posted 14 April 2015 - 01:50 PM

View PostKingofGamesYami, on 14 April 2015 - 01:13 PM, said:

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!

#5 minebuild02

  • Members
  • 97 posts

Posted 14 April 2015 - 01:55 PM

Now I've got another problem: How do I remove a string from the file?

#6 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 14 April 2015 - 01: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.

#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 14 April 2015 - 02:02 PM

It may be worth reading through This thread.

#8 minebuild02

  • Members
  • 97 posts

Posted 14 April 2015 - 05:11 PM

View PostKingofGamesYami, on 14 April 2015 - 02:02 PM, said:

It may be worth reading through This thread.
Thanks

#9 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 14 April 2015 - 06:14 PM

you could also do this
local afterItGotRemoved = string.replace(beforeItGotRemoved,whatToRemove," ")


#10 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 14 April 2015 - 06:29 PM

Nope, no string.replace function. Perhaps you meant string.gsub?





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users