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

fs Question

Started by Mr_Programmer, 31 May 2014 - 11:38 AM
Mr_Programmer #1
Posted 31 May 2014 - 01:38 PM
if i have multiple data in a file that fs is reading… lets say
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?
KingofGamesYami #2
Posted 31 May 2014 - 02:14 PM
I haven't tried this, but I think it should work:

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()

I used a pattern to identify the first line break, and then read the rest of the file.
Mr_Programmer #3
Posted 31 May 2014 - 02:17 PM
I haven't tried this, but I think it should work:

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()

I used a pattern to identify the first line break, and then read the rest of the file.
Thanks! May i ask what is this code doing, like what does the "%\n%.*" do?
Lyqyd #4
Posted 31 May 2014 - 02:21 PM
It's gonna be a hell of a lot easier to read the file out into a table, change the appropriate index, then write out the table as each line again.


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
KingofGamesYami #5
Posted 31 May 2014 - 02:21 PM
-snip-
Thanks! May i ask what is this code doing, like what does the "%\n%.*" do?
That is a pattern, which I linked the page to.
%\n% specifically finds the character \n, which in lua is a new line character.
%.* identifies 0 or more of any character, because "." represents all characters and "*" is an operator that finds 0 or more repetitions.

Edit: ninja'd
Edited on 31 May 2014 - 12:22 PM
Mr_Programmer #6
Posted 31 May 2014 - 02:27 PM
It's gonna be a hell of a lot easier to read the file out into a table, change the appropriate index, then write out the table as each line again.


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
sorry to be a pain but could you explane this code and whats its doing, im new at the whole fs API and tables