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

How to remove a certain line of code from FS

Started by TNT_Nolan, 19 May 2016 - 09:25 PM
TNT_Nolan #1
Posted 19 May 2016 - 11:25 PM
How do I make a code to remove a string of data from another file.

Main

file = fs.open("settings","r")
setting1 = file.readLine(1)
file.close
print("Current setting for background is "..setting1.." would you like to change it?")
result = doButtonsYN()
if result == true then
      term.clear()
      term.setCursorPos(1,1)
      print("Enter new color for setting")
      newColor = read()
      --Remove Line one of the settings file

      file = fs.open("settings","a")
      file.write(newColor)
      print("Fixing Color")
else
 return
end

settings

colors.red
colors.blue
colors.yellow
--/\ Colors Settings 
-- |

Response would be appricated.

Thanks,
Nolan
Bomb Bloke #2
Posted 19 May 2016 - 11:34 PM
file.readLine() doesn't expect any arguments and ignores them if provided. It simply returns a line each time you call it, starting from the top of the file and working down until it runs out (at which point it starts returning nil).

If you want to remove content from a file, you'll need to load all the previous content into RAM, edit it there, and then re-write the file with the new revision.

local fileContent = {}

for line in io:lines("settings") do
  fileContent[#fileContent + 1] = line
end

fileContent[1] = read()

local file = fs.open("settings", "w")

for i = 1, #fileContent do
  file.writeLine(fileContent[i])
end

file.close()