test test2 test3 test4
how would i just change the first line "test" and not change the rest, i know how to open the file and the modes etc...
i was just wondering if thats posiable to change the first line and not do anything else with the others?
Posted 31 May 2014 - 12:38 PM
test test2 test3 test4
Posted 31 May 2014 - 01:14 PM
local file = fs.open("yourfilename", "r")
local data = file.readAll()
file.close()
local newdata = "lineOne\n"..data:gsub("%\n%.*")
local file = fs.open("yourfilename", "w")
file.writeLine(newdata)
file.close()
Posted 31 May 2014 - 01:17 PM
KingofGamesYami, on 31 May 2014 - 01:14 PM, said:
local file = fs.open("yourfilename", "r")
local data = file.readAll()
file.close()
local newdata = "lineOne\n"..data:gsub("%\n%.*")
local file = fs.open("yourfilename", "w")
file.writeLine(newdata)
file.close()
Posted 31 May 2014 - 01:21 PM
local fileTable = {}
local handle = io.open("myFile", "r")
if handle then
for line in handle:lines() do
fileTable[#fileTable + 1] = line
end
handle:close()
end
fileTable[1] = "new value for first line"
handle = io.open("myFile", "w")
if handle then
for _, line in ipairs(fileTable) do
handle:write(line.."\n")
end
handle:close()
end
Posted 31 May 2014 - 01:21 PM
Mr_Programmer, on 31 May 2014 - 01:17 PM, said:
Edited by KingofGamesYami, 31 May 2014 - 01:22 PM.
Posted 31 May 2014 - 01:27 PM
Lyqyd, on 31 May 2014 - 01:21 PM, said:
local fileTable = {}
local handle = io.open("myFile", "r")
if handle then
for line in handle:lines() do
fileTable[#fileTable + 1] = line
end
handle:close()
end
fileTable[1] = "new value for first line"
handle = io.open("myFile", "w")
if handle then
for _, line in ipairs(fileTable) do
handle:write(line.."\n")
end
handle:close()
end
0 members, 2 guests, 0 anonymous users