It would be better to iterate through the table and write each line:
local function readLines(sPath)
local file = fs.open(sPath, "r")
if file then
local tLines = {}
local sLine = file.readLine()
while sLine do
table.insert(tLines, sLine)
sLine = file.readLine()
end
file.close()
return tLines
end
return nil
end
local function writeLines(sPath, tLines)
local file = fs.open(sPath, "w")
if file then
for _, sLine in ipairs(tLines) do
file.writeLine(sLine)
end
file.close()
end
end
You can then use those functions like:
local tLines = readLines("filename")
print("Lines in the file: ", #tLines)
tLines[2] = "This is line 2!"
table.insert(tLines, "This is the last line!")
writeLines("filename", tLines)
Note: you should try to understand the functions, not just copy them. If there's something you don't understand, just ask.