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

help with fs api

Started by tball146, 17 April 2016 - 03:26 PM
tball146 #1
Posted 17 April 2016 - 05:26 PM
okay i made a file manager thingy you can overwrite files code but the issue is that it erases all non-overwrited code aswell where as i just wanna modify a line of that code
example:


noob=false
if noob then
print'noob!'
else
print'no'
end

after i use the file manager it should be:

noob=true
if noob then
print'noob!'
else
print'no'

not:

noob=true


thanks for your assistance

P.S this may get edited alot sorry i tend to not see my errors
Edited on 17 April 2016 - 03:33 PM
Lupus590 #2
Posted 17 April 2016 - 05:42 PM
when you open a file for writing you erase the file and then start writing

if you want to keep the rest of the file you have to write the rest of it back (even if you didn't change it)


local f = fs.open("fileName", "r")
local data = f.readAll()

f.close()
f = fs.open("fileName", "w") --#the file is now blank, wiped clean for our new data

f.writeLine(add your stuff here)

--#we have added the new data to the top of the file, time to restore the rest of it
for k,v in ipairs(data) do
  f.writeLine(v)
end

tball146 #3
Posted 17 April 2016 - 05:53 PM
thank you i plan to release the file manger soon today