Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function editLine(file, line, content)
- local lines = {}
- local num,d = 0,nil
- if fs.isReadOnly(file) then
- error("File is readonly",2)
- end
- if not type(content) == "string" then
- error("bad argument: string expected, got "..type(content), 2)
- end
- if not type(file) == "string" then
- error("bad argument: string expected, got "..type(file), 2)
- end
- if not type(line) == "number" then
- error("bad argument: number expected, got "..type(line), 2)
- end
- if not fs.exists(file) then
- error("File doesn't exist",2)
- end
- if fs.isDir(file) then
- error("File is directory",2)
- end
- num = num-1
- if num > line then
- error("File smaller than "..line.." lines long.",2)
- end
- local reading = fs.open(file, "r")
- while true do
- f=nil
- f = reading.readLine()
- if not f then break end
- table.insert(lines, f)
- end
- reading.close()
- reading = nil
- lines[line] = content
- if num > #lines-1 then
- error("File smaller than "..line.." lines long.",2)
- end
- local write = fs.open(file, "w")
- for i=1, #lines do
- write.writeLine(lines[i])
- end
- write.close()
- write = nil
- return true
- end
Advertisement
Add Comment
Please, Sign In to add comment