Jump to content




fs Question


  • You cannot reply to this topic
5 replies to this topic

#1 Mr_Programmer

  • Members
  • 95 posts
  • LocationA CPU some where in Bolton, UK

Posted 31 May 2014 - 12: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?

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 31 May 2014 - 01: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.

#3 Mr_Programmer

  • Members
  • 95 posts
  • LocationA CPU some where in Bolton, UK

Posted 31 May 2014 - 01:17 PM

View PostKingofGamesYami, on 31 May 2014 - 01:14 PM, said:

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?

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 31 May 2014 - 01: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


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 31 May 2014 - 01:21 PM

View PostMr_Programmer, on 31 May 2014 - 01:17 PM, said:

-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 by KingofGamesYami, 31 May 2014 - 01:22 PM.


#6 Mr_Programmer

  • Members
  • 95 posts
  • LocationA CPU some where in Bolton, UK

Posted 31 May 2014 - 01:27 PM

View PostLyqyd, on 31 May 2014 - 01:21 PM, said:

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





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users