Bomb Bloke, on 28 December 2014 - 02:35 PM, said:
Long story short, there's no method (within ComputerCraft) of opening a file and editing just a part of it. You need to load the whole file into memory (RAM), alter it there, and then re-write the entire thing back to disk.
To expand on this...
Let's say you want to change line 5 of a file. Rather than saying "write to line 5", you need to read the entire contents of the file, change line 5, then write everything back in. You can use this by using
h.readLine().
local lines = {}
local h = fs.open( path, "r" )
for line in h.readLine do
lines[#lines+1] = line
end
h.close()
Now you have a table of the lines of a file. Line 1 would be
lines[1].
You can also change an individual line by doing
lines[i] = x.
To update the file you got it from, you need to write back all the lines that you read (and potentially modified):
local h = fs.open( path, "w" )
for i = 1, #lines do
h.writeLine( lines[i] )
end
h.close()